Skip to content

Implement __eq__ for remaining standard-library gates except mcx #13327

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 15, 2024

Conversation

jakelishman
Copy link
Member

Summary

Most standard-library gates had a specialised __eq__ added in gh-11370, but a small number were left over. This adds specialisation to almost all the stragglers, which were mostly old soft-deperecated gates. Despite not appearing in most circuits, these gates have an outsized effect on equality comparisons if they ever do appear, because they involve construction of entire definition fields.

Details and comments

You can see the improvement here in the OQ3 exporter for circuits containing some of the affected gates; it uses gate equality at one part of the symbol lookup. For example, given this circuit:

from qiskit.circuit.random import random_circuit
from qiskit import qasm3

circ = random_circuit(200, 200, seed=42)

The time of qasm3.dumps(circ) on my machine went from about 1.15(2)s to 0.76(2)s with this PR, despite only about 9% of the gates in the circuit being affected by this PR.

Most standard-library gates had a specialised `__eq__` added in
Qiskitgh-11370, but a small number were left over.  This adds specialisation
to almost all the stragglers, which were mostly old soft-deperecated
gates.  Despite not appearing in most circuits, these gates have an
outsized effect on equality comparisons if they ever _do_ appear,
because they involve construction of entire `definition` fields.
@jakelishman jakelishman added performance Changelog: New Feature Include in the "Added" section of the changelog mod: qasm3 Related to OpenQASM 3 import or export mod: circuit Related to the core of the `QuantumCircuit` class or the circuit library labels Oct 15, 2024
@jakelishman jakelishman added this to the 1.3.0 milestone Oct 15, 2024
@jakelishman jakelishman requested a review from a team as a code owner October 15, 2024 17:17
@qiskit-bot
Copy link
Collaborator

One or more of the following people are relevant to this code:

  • @Cryoris
  • @Qiskit/terra-core
  • @ajavadia

@coveralls
Copy link

Pull Request Test Coverage Report for Build 11351094849

Details

  • 16 of 16 (100.0%) changed or added relevant lines in 5 files are covered.
  • 14 unchanged lines in 3 files lost coverage.
  • Overall coverage increased (+0.01%) to 88.665%

Files with Coverage Reduction New Missed Lines %
qiskit/transpiler/passes/synthesis/unitary_synthesis.py 2 88.19%
crates/qasm2/src/lex.rs 5 92.23%
qiskit/synthesis/two_qubit/xx_decompose/decomposer.py 7 90.77%
Totals Coverage Status
Change from base Build 11349069953: 0.01%
Covered Lines: 73185
Relevant Lines: 82541

💛 - Coveralls

Copy link
Contributor

@Cryoris Cryoris left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍🏻

Just two questions:

  • Would it make sense to move __eq__ just to Gate and ControlledGate (with the two special cases of MCPhase/U1 that adds the num_ctrl_qubits check)?
  • I assume you didn't add it to MCX due to all the different gate flavors, right? If yes then we should remember to add this once the flavors are gone and we just have the MCXGate block 🙂

@jakelishman
Copy link
Member Author

jakelishman commented Oct 15, 2024

The trouble is that these specialised __eq__ methods need to be opt-in, when the type is certain that there are no other modifications. The base Gate and ControlledGate can't know that it's safe to override this; they won't know how the _define method is implemented, so they need to fail safer (which is actually what they do). These checks don't need to assert tests on the definitions, because they can know equality without creating them.

For mcx: yeah, I didn't want to get into the can of worms of all the different variants, and what should be considered == between them.

@Cryoris Cryoris added this pull request to the merge queue Oct 15, 2024
Merged via the queue into Qiskit:main with commit 494e4bb Oct 15, 2024
15 checks passed
@jakelishman jakelishman deleted the circuit-library-missing-eq branch October 15, 2024 20:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: New Feature Include in the "Added" section of the changelog mod: circuit Related to the core of the `QuantumCircuit` class or the circuit library mod: qasm3 Related to OpenQASM 3 import or export performance
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants