Skip to content

Fix global phase tracking around DAGCircuit.substitute_node.#5618

Merged
mergify[bot] merged 7 commits into
Qiskit:masterfrom
kdk:dagcircuit-fix-substitute-node-global-phase-usage
Jan 21, 2021
Merged

Fix global phase tracking around DAGCircuit.substitute_node.#5618
mergify[bot] merged 7 commits into
Qiskit:masterfrom
kdk:dagcircuit-fix-substitute-node-global-phase-usage

Conversation

@kdk
Copy link
Copy Markdown
Member

@kdk kdk commented Jan 12, 2021

Summary

Updates the Unroller and BasisTranslator passes to only manually update the global phase of the target dag when using DAGCircuit.substitute_node, as DAGCircuit.substitute_node_with_dag already handles updating global phase of the target dag.

Details and comments

Also updates DAGCircuit.__eq__ to check equality of the global_phase and calibrations attributes and updates tests. Also fixes a case where a QuantumCirucit.global_phase which had been set from as a ParameterExpression could avoid the [0, 2pi) bounds check.

@kdk kdk added on hold Can not fix yet stable backport potential Make Mergify open a backport PR to the most recent stable branch on merge. global-phase labels Jan 12, 2021
@kdk kdk added this to the 0.17 milestone Jan 12, 2021
@kdk kdk requested a review from a team as a code owner January 12, 2021 23:02
@kdk kdk added the Changelog: Fixed Add a "Fixed" entry in the GitHub Release changelog. label Jan 12, 2021
@kdk kdk force-pushed the dagcircuit-fix-substitute-node-global-phase-usage branch from e310c4f to 19f9d5f Compare January 20, 2021 15:59
@kdk kdk force-pushed the dagcircuit-fix-substitute-node-global-phase-usage branch from 19f9d5f to 8cbee03 Compare January 20, 2021 21:34
@kdk kdk removed the on hold Can not fix yet label Jan 20, 2021
@kdk kdk linked an issue Jan 20, 2021 that may be closed by this pull request
@kdk kdk force-pushed the dagcircuit-fix-substitute-node-global-phase-usage branch from 8cbee03 to b70256d Compare January 20, 2021 21:51
mtreinish
mtreinish previously approved these changes Jan 20, 2021
Copy link
Copy Markdown
Member

@mtreinish mtreinish 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 a couple of inline comments. Nothing worth blocking over. But it might be worth having someone else check the global phase adjustments in the tests. I always seem to get them wrong. :)

circuit = QuantumCircuit(2)
circuit.append(random_unitary(4, seed=1234), [0, 1])
circuit = circuit.decompose()
circuit.cz(0, 1)
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.

Was this change made because the random unitary decomposition to qasm and back would lose the global phase and the equality would differ now? FWIW I agree using a random unitary here seems unnecessary since it's testing the pi serialization for qasm, I'm just curious.

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.

That's right, the unitary ends up generating a non-zero global phase that's lost in the back and forth to QASM.

…-usage-ee05476a7d0b24c6.yaml

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
…-usage-ee05476a7d0b24c6.yaml

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
@itoko
Copy link
Copy Markdown
Contributor

itoko commented Jan 21, 2021

I checked all the global_phase values in the tests and looks correct to me. (I updated this comment, in my original comment, I missed your update on DAGCircuit.__eq__)

Copy link
Copy Markdown
Member

@mtreinish mtreinish left a comment

Choose a reason for hiding this comment

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

LGTM thanks @kdk for updating and @itoko thanks for checking the global phase values.

@mergify mergify Bot merged commit 89cd41e into Qiskit:master Jan 21, 2021
kdk added a commit that referenced this pull request Jan 21, 2021
* DAGCircuit.__eq__ to check .global_phase and .calibrations.

* Fix BasisTranslator and Unroller global phase for substitute_node.

* Update releasenotes/notes/dagcircuit-fix-substitute-node-global-phase-usage-ee05476a7d0b24c6.yaml

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>

* Update releasenotes/notes/dagcircuit-fix-substitute-node-global-phase-usage-ee05476a7d0b24c6.yaml

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
(cherry picked from commit 89cd41e)
mtreinish added a commit to mtreinish/qiskit-core that referenced this pull request Jan 25, 2021
In Qiskit#5618 the dagcircuit __eq__ method was updated to also compare the
global_phase and calibrations property. This was done to ensure we
preserve both through a transpilation and other transforms since when we
assert a circuit is equal to the expect result it will also check phase.
However, this has backwards compatibility implications since comparing 2
QuantumCircuits would previously ignore global phase. This commit
removes this piece from the backport and just leave the phase
corrections as the backport.
mergify Bot added a commit that referenced this pull request Jan 25, 2021
… (#5673)

* Fix global phase tracking around DAGCircuit.substitute_node. (#5618)

* DAGCircuit.__eq__ to check .global_phase and .calibrations.

* Fix BasisTranslator and Unroller global phase for substitute_node.

* Update releasenotes/notes/dagcircuit-fix-substitute-node-global-phase-usage-ee05476a7d0b24c6.yaml

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>

* Update releasenotes/notes/dagcircuit-fix-substitute-node-global-phase-usage-ee05476a7d0b24c6.yaml

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
(cherry picked from commit 89cd41e)

* Backport global phase from #5517 on grover operator

This commit adds a missing phase of pi to the grover operator circuit
library entry. It is a backport from #5517.

* Remove backport of phase and calibrartions check to dagcircuit eq

In #5618 the dagcircuit __eq__ method was updated to also compare the
global_phase and calibrations property. This was done to ensure we
preserve both through a transpilation and other transforms since when we
assert a circuit is equal to the expect result it will also check phase.
However, this has backwards compatibility implications since comparing 2
QuantumCircuits would previously ignore global phase. This commit
removes this piece from the backport and just leave the phase
corrections as the backport.

Co-authored-by: Kevin Krsulich <kevin.krsulich@ibm.com>
Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
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. stable backport potential Make Mergify open a backport PR to the most recent stable branch on merge.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Global phase not in [0, 2pi)

3 participants