Skip to content

Add IGate rules to standard equivalence library#7146

Merged
jakelishman merged 10 commits into
Qiskit:mainfrom
mtreinish:id-equivalence-lib
Oct 16, 2023
Merged

Add IGate rules to standard equivalence library#7146
jakelishman merged 10 commits into
Qiskit:mainfrom
mtreinish:id-equivalence-lib

Conversation

@mtreinish
Copy link
Copy Markdown
Member

@mtreinish mtreinish commented Oct 15, 2021

Summary

This commit adds a missing rules to the equivalence library for an
identity gate. Without this the basis translator has difficulty finding
a match if the basis does not include an id gate (see
Qiskit/qiskit-metapackage#1346)

Details and comments

Fix #9485.

This commit adds a missing rules to the equivalence library for an
identity gate. Without this the basis translator has difficulty finding
a match if the basis does not include an id gate (see
Qiskit#1346)
@mtreinish mtreinish requested a review from a team as a code owner October 15, 2021 14:33
jakelishman
jakelishman previously approved these changes Oct 15, 2021
Copy link
Copy Markdown
Member

@jakelishman jakelishman left a comment

Choose a reason for hiding this comment

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

These seem all fine. In normal transpiler runs, will these even activate? It feels like IGate should generally just get removed from the circuit (unless I suppose it has calibrations/noise models attached to it, but then the equivalences shouldn't run either).

(I think black doesn't like your single quotes)

jakelishman
jakelishman previously approved these changes Oct 15, 2021
@mtreinish
Copy link
Copy Markdown
Member Author

These seem all fine. In normal transpiler runs, will these even activate? It feels like IGate should generally just get removed from the circuit (unless I suppose it has calibrations/noise models attached to it, but then the equivalences shouldn't run either).

It actually would come up if there is an id gate in the input circuit and not in the basis set at all optimization levels I think. Since we run the basis translator prior to any optimizations that will remove the id gate.

I specifically hit this in a pretty synthetic case though, in the benchmarks I'm adding in: #1346 which just uses random_circuit() and just runs the basis translator bare on it using a basis without an id gate because random_circuit() just randomly picks gates from the standard library and inserts them into the circuit and I was ending up with some id gates in the circuit which caused the basis translator to stall since it couldn't find an equivalence.

@jakelishman
Copy link
Copy Markdown
Member

Yeah, I'd seen the synthetic benchmark. I suppose it makes sense that the basis-gate translator would be run first; if we ran the hypothetical "check for no-ops" too early, we'd probably have to run it again later to make sure we hadn't introduced any more by cancellations or parameter combinations or something.

Copy link
Copy Markdown
Member

@kdk kdk left a comment

Choose a reason for hiding this comment

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

@mtreinish
Copy link
Copy Markdown
Member Author

Comments in Qiskit/qiskit#1346 (comment) .

This shouldn't hold true anymore as new backends will no longer support the id instruction. It's also been raising a warning for > a year if a user submits a circuit to the ibmq provider with an id gate right?

@coveralls
Copy link
Copy Markdown

coveralls commented Dec 1, 2022

Pull Request Test Coverage Report for Build 3658676946

  • 16 of 16 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.003%) to 84.56%

Totals Coverage Status
Change from base Build 3658575673: 0.003%
Covered Lines: 63238
Relevant Lines: 74785

💛 - Coveralls

@mtreinish
Copy link
Copy Markdown
Member Author

Additionally I just pushed: Qiskit/qiskit-ibm-provider#468 to have IBM backend's preserve their custom definition of id == delay(sx.duration) through transpile via custom backend plugin defaults. This means that combined with this PR IBM backends get their custom behavior and for all other backends id == identity (especially when combined with #7403 )

jakelishman
jakelishman previously approved these changes Jan 11, 2023
Copy link
Copy Markdown
Member

@jakelishman jakelishman left a comment

Choose a reason for hiding this comment

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

Do we need to supply equivalences to other no-op gates, or do you think it would be suitable to just supply and equivalence to the empty 1q DAG?

If we do need the no-op gates, then this seems fine to me (pending Kevin's review as well).

@kdk kdk changed the title Add IGate rules to session equivalence library Add IGate rules to standard equivalence library Jan 12, 2023
Comment thread qiskit/circuit/library/standard_gates/equivalence_library.py
@kdk
Copy link
Copy Markdown
Member

kdk commented Jan 12, 2023

Additionally I just pushed: Qiskit/qiskit-ibm-provider#468 to have IBM backend's preserve their custom definition of id == delay(sx.duration) through transpile via custom backend plugin defaults. This means that combined with this PR IBM backends get their custom behavior and for all other backends id == identity (especially when combined with #7403 )

I'm a bit confused as to why Qiskit/qiskit-ibm-provider#468 is needed in addition to the this PR and #7403. Wouldn't that override this PR and #7403 to reinstate the deprecated behavior (and maybe silence the deprecation warning)?

@jakelishman
Copy link
Copy Markdown
Member

Yeah, that's the idea: the old backends that do maintain that definition translate id into the delay they associate with it themselves, so the rest of the transpiler stack can move away from it, without affecting the old backends.

This and #7403 are about not enforcing that custom behaviour on every other hardware backend, and letting various other parts of Qiskit now reason about the identity operation at a high-level in the expected way.

@kdk
Copy link
Copy Markdown
Member

kdk commented Jan 12, 2023

Yeah, that's the idea: the old backends that do maintain that definition translate id into the delay they associate with it themselves, so the rest of the transpiler stack can move away from it, without affecting the old backends.

I think I'm a bit lost on the motivation. It might be a good time to write down all the cases we're thinking of, and the various timelines and expected behaviors. Why would the backends that still maintain 'id' want or need a conversion into the equivalent 'delay'? Is the expectation that these backends will maintain an 'id' until they're retired?

@mtreinish mtreinish removed this from the 0.23.0 milestone Jan 19, 2023
@mtreinish
Copy link
Copy Markdown
Member Author

This is superseded by #7403 as I don't think we need explicit equivalence lib entries for IGate if it has a no-op definition

@mtreinish mtreinish closed this Mar 31, 2023
@mtreinish mtreinish reopened this Jul 19, 2023
Adding the id equivalence rules to the equivalence library is changing
the output translation for one of the tests and this causes the test
which is using an exact circuit comparison to fail. This commit updates
the test to use the new output but then also does an operator comparison
to ensure that the output circuit is equivalent too.
Copy link
Copy Markdown
Member

@jakelishman jakelishman left a comment

Choose a reason for hiding this comment

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

A minor question, but I'm in general happy to merge.

@jakelishman jakelishman requested a review from kdk September 11, 2023 16:56
@jakelishman jakelishman added this to the 0.45.0 milestone Sep 11, 2023
@coveralls
Copy link
Copy Markdown

coveralls commented Sep 11, 2023

Pull Request Test Coverage Report for Build 6424802268

  • 16 of 16 (100.0%) changed or added relevant lines in 1 file are covered.
  • 7 unchanged lines in 3 files lost coverage.
  • Overall coverage increased (+0.001%) to 87.027%

Files with Coverage Reduction New Missed Lines %
crates/qasm2/src/expr.rs 1 93.76%
crates/qasm2/src/lex.rs 3 90.91%
qiskit/pulse/library/waveform.py 3 93.75%
Totals Coverage Status
Change from base Build 6414428479: 0.001%
Covered Lines: 74184
Relevant Lines: 85243

💛 - Coveralls

Comment on lines 932 to 938
expected = QuantumCircuit(2)
expected.u(pi / 2, 0, pi, qr[0])
expected.u(0, 0, -pi / 2, qr[0])
expected.u(-pi / 2, -pi / 2, pi / 2, qr[1])
expected.u(pi, 0, 0, qr[0])
expected.u(pi / 2, -pi / 2, pi / 2, qr[1])
expected.ecr(0, 1)
expected.u(pi, 0, pi, qr[0])
expected_dag = circuit_to_dag(expected)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

My question from the prior review! It got lost before - I must have forgotten to save the comment.

Do we know why this changes? We haven't added any rules that go to IGate, so I'd not have expected the basis-translation path to change.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

TBH, I'm not sure. I didn't really think to check. I just saw the output circuit was equivalent and updated the test. I'll do some debugging locally because the qpy tests are failing in a weird way now too so this is blocked on that anyway.

Copy link
Copy Markdown
Member

@kdk kdk left a comment

Choose a reason for hiding this comment

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

Following some updates since this was last discussed (id will stick around on IBM backends, and is being defined in Qiskit as a single-qubit no-op of backend-defined duration), I think this is good to go (pending #7146 (comment) ).

It would also be good to call out (in the IGate docstring and maybe as a release note) that IGate is no longer strictly opaque, and should be expected to be optimized away by the transpiler except at optimization_level=0.

@jakelishman jakelishman enabled auto-merge October 16, 2023 19:31
@jakelishman jakelishman added this pull request to the merge queue Oct 16, 2023
Merged via the queue into Qiskit:main with commit e92b0df Oct 16, 2023
@mtreinish mtreinish deleted the id-equivalence-lib branch October 17, 2023 01:37
@ElePT ElePT added the Changelog: Fixed Add a "Fixed" entry in the GitHub Release changelog. label Nov 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Changelog: Fixed Add a "Fixed" entry in the GitHub Release changelog.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

transpile function with basis_gates=['u', 'cx'] fails with any circuit with an id gate

5 participants