Skip to content
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

Fix UnitaryGate.qasm() with unused qubits #8234

Merged
merged 2 commits into from
Jun 24, 2022

Conversation

jakelishman
Copy link
Member

The OpenQASM 2 exporter has a special case for providing gate
definitions, which was implemented purely for UnitaryGate. This is
stateful per instruction, and does not generally behave well with the
rest of the exporter. This commit fixes one misbehaviour: the way the
qubit operands for the definition was calculated was unreliable in both
order and number of qubits, if any of the qubits were unaffected by
the gate operation.

Details and comments

There are general problems with the special-case approach to defining the definition like this (see also #8222), which this commit does not fix, and the whole OpenQASM 2 exporter is known to be pretty questionable with custom and parameterised gates still.

Fix #8224.

The OpenQASM 2 exporter has a special case for providing `gate`
definitions, which was implemented purely for `UnitaryGate`.  This is
stateful per instruction, and does not generally behave well with the
rest of the exporter.  This commit fixes one misbehaviour: the way the
qubit operands for the definition was calculated was unreliable in both
order and number of qubits, if any of the qubits were unaffected by
the gate operation.
@jakelishman jakelishman added the Changelog: Bugfix Include in the "Fixed" section of the changelog label Jun 24, 2022
@jakelishman jakelishman requested a review from a team as a code owner June 24, 2022 12:36
@qiskit-bot
Copy link
Collaborator

Thank you for opening a new pull request.

Before your PR can be merged it will first need to pass continuous integration tests and be reviewed. Sometimes the review process can be slow, so please be patient.

While you're waiting, please feel free to review other open PRs. While only a subset of people are authorized to approve pull requests for merging, everyone is encouraged to review open pull requests. Doing reviews helps reduce the burden on the core team and helps make the project's code better for everyone.

One or more of the the following people are requested to review this:

  • @Qiskit/terra-core

@mtreinish mtreinish added the stable backport potential The bug might be minimal and/or import enough to be port to stable label Jun 24, 2022
@mtreinish mtreinish added this to the 0.21 milestone Jun 24, 2022
Copy link
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, this fix is straightforward. The handling of unitary gate in qasm 2.0 was always a bit odd, but this at least fixes the obvious bug around input qubit parameters

@coveralls
Copy link

coveralls commented Jun 24, 2022

Pull Request Test Coverage Report for Build 2557223060

  • 1 of 1 (100.0%) changed or added relevant line in 1 file are covered.
  • 3 unchanged lines in 1 file lost coverage.
  • Overall coverage decreased (-0.006%) to 83.951%

Files with Coverage Reduction New Missed Lines %
qiskit/pulse/library/waveform.py 3 89.36%
Totals Coverage Status
Change from base Build 2557221023: -0.006%
Covered Lines: 55744
Relevant Lines: 66401

💛 - Coveralls

@mergify mergify bot merged commit 8d5e3ca into Qiskit:main Jun 24, 2022
mergify bot pushed a commit that referenced this pull request Jun 24, 2022
The OpenQASM 2 exporter has a special case for providing `gate`
definitions, which was implemented purely for `UnitaryGate`.  This is
stateful per instruction, and does not generally behave well with the
rest of the exporter.  This commit fixes one misbehaviour: the way the
qubit operands for the definition was calculated was unreliable in both
order and number of qubits, if any of the qubits were unaffected by
the gate operation.

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
(cherry picked from commit 8d5e3ca)
mergify bot added a commit that referenced this pull request Jun 24, 2022
The OpenQASM 2 exporter has a special case for providing `gate`
definitions, which was implemented purely for `UnitaryGate`.  This is
stateful per instruction, and does not generally behave well with the
rest of the exporter.  This commit fixes one misbehaviour: the way the
qubit operands for the definition was calculated was unreliable in both
order and number of qubits, if any of the qubits were unaffected by
the gate operation.

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
(cherry picked from commit 8d5e3ca)

Co-authored-by: Jake Lishman <jake.lishman@ibm.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
@@ -191,22 +191,13 @@ def qasm(self):
_qasm_escape_gate_name(self.label) if self.label else "unitary" + str(id(self))
)

# map from gates in the definition to params in the method
bit_to_qasm = OrderedDict()

Choose a reason for hiding this comment

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

The OrderedDict() class is no longer being used, it should be better to remove that import to avoid any import issues.

Choose a reason for hiding this comment

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

This is giving an error in further tests done using tox -elint due to it being an unused-import and can cause problems for future developers when running tests.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks, this was a good catch. I'm not sure how this slipped through CI here (I relied a bit too heavily on it in my code review earlier) but this also just started failing on CI today, the fix for this is here: #8366

mtreinish added a commit to mtreinish/qiskit-core that referenced this pull request Jul 18, 2022
Recently the pylint CI runs have all started failing because of an
unused OrderedDict import failure in qiskit/extensions/unitary.py. The
OrderedDict usage was removed in Qiskit#8234 but the import was left in
accidently. This should have been caught by the lint job on that PR but
for some reason it was not caught and has sat there until relatively
recently when pylint started erroring because of the error. Normally for
failures like this they can be attributed to environment differences,
typically a release of pylint or astroid. However, we pin those package
versions because of their tendancy to change behavior, and also a diff
between the installed python packages in CI doesn't show any differences.
Regardless of the underlying cause to unblock CI this commit is necessary
to fix the unused import error.
Cryoris pushed a commit that referenced this pull request Jul 19, 2022
Recently the pylint CI runs have all started failing because of an
unused OrderedDict import failure in qiskit/extensions/unitary.py. The
OrderedDict usage was removed in #8234 but the import was left in
accidently. This should have been caught by the lint job on that PR but
for some reason it was not caught and has sat there until relatively
recently when pylint started erroring because of the error. Normally for
failures like this they can be attributed to environment differences,
typically a release of pylint or astroid. However, we pin those package
versions because of their tendancy to change behavior, and also a diff
between the installed python packages in CI doesn't show any differences.
Regardless of the underlying cause to unblock CI this commit is necessary
to fix the unused import error.
mergify bot pushed a commit that referenced this pull request Jul 19, 2022
Recently the pylint CI runs have all started failing because of an
unused OrderedDict import failure in qiskit/extensions/unitary.py. The
OrderedDict usage was removed in #8234 but the import was left in
accidently. This should have been caught by the lint job on that PR but
for some reason it was not caught and has sat there until relatively
recently when pylint started erroring because of the error. Normally for
failures like this they can be attributed to environment differences,
typically a release of pylint or astroid. However, we pin those package
versions because of their tendancy to change behavior, and also a diff
between the installed python packages in CI doesn't show any differences.
Regardless of the underlying cause to unblock CI this commit is necessary
to fix the unused import error.

(cherry picked from commit 007a746)
mergify bot added a commit that referenced this pull request Jul 19, 2022
Recently the pylint CI runs have all started failing because of an
unused OrderedDict import failure in qiskit/extensions/unitary.py. The
OrderedDict usage was removed in #8234 but the import was left in
accidently. This should have been caught by the lint job on that PR but
for some reason it was not caught and has sat there until relatively
recently when pylint started erroring because of the error. Normally for
failures like this they can be attributed to environment differences,
typically a release of pylint or astroid. However, we pin those package
versions because of their tendancy to change behavior, and also a diff
between the installed python packages in CI doesn't show any differences.
Regardless of the underlying cause to unblock CI this commit is necessary
to fix the unused import error.

(cherry picked from commit 007a746)

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
@jakelishman jakelishman deleted the fix/qasm2-identity-unitary branch August 19, 2022 12:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: Bugfix Include in the "Fixed" section of the changelog stable backport potential The bug might be minimal and/or import enough to be port to stable
Projects
None yet
5 participants