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

raise an exception with a custom gate with clbits or no qubits #10438

Merged
merged 10 commits into from
Jul 24, 2023

Conversation

1ucian0
Copy link
Member

@1ucian0 1ucian0 commented Jul 16, 2023

Fixes #10435
Fix #7351

Currently, OpenQASM 2 exporter dumps invalid code when a custom gate has no qubits or has classical wires involved.

The no-qubit case it is considered invalid by the OpenQASM 2 grammar:

<gatedecl> |= gate <id> <idlist> { ...
<idlist> |= <id> | <idlist> , <id>

I think the case of <id> referring to classical wires can be inferred from the name of the statement: gate, as in a quantum gate.

@1ucian0 1ucian0 added the mod: qasm2 Relating to OpenQASM 2 import or export label Jul 16, 2023
@1ucian0
Copy link
Member Author

1ucian0 commented Jul 16, 2023

before adding tests and reno, @jakelishman do you think adding this limitation makes sense?

@coveralls
Copy link

coveralls commented Jul 16, 2023

Pull Request Test Coverage Report for Build 5643565062

  • 6 of 6 (100.0%) changed or added relevant lines in 1 file are covered.
  • 9 unchanged lines in 3 files lost coverage.
  • Overall coverage remained the same at 85.926%

Files with Coverage Reduction New Missed Lines %
qiskit/extensions/unitary.py 1 93.75%
crates/qasm2/src/lex.rs 2 91.39%
crates/qasm2/src/parse.rs 6 97.58%
Totals Coverage Status
Change from base Build 5631006520: 0.0%
Covered Lines: 73022
Relevant Lines: 84982

💛 - Coveralls

Comment on lines 5093 to 5099
# Gate definitions with 0 qubits or with any classical bits are not allowed.
if operation.num_qubits == 0 or operation.num_clbits != 0:
raise CircuitError(
"OpenQASM 2 does not allow gate definitions with no qubits or with any "
f"classical bit: '{operation.name}' has {operation.num_qubits} qubits "
f"and {operation.num_clbits} clbits"
)
Copy link
Member

Choose a reason for hiding this comment

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

The error should be QASM2ExportError, but otherwise this is fine.

Not sure the comment is needed, since the error message says the exact same thing ;)

Copy link
Member Author

@1ucian0 1ucian0 Jul 20, 2023

Choose a reason for hiding this comment

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

Good catch. Thanks! done in fc0d3be

@1ucian0 1ucian0 marked this pull request as ready for review July 24, 2023 06:19
@1ucian0 1ucian0 requested a review from a team as a code owner July 24, 2023 06:19
@qiskit-bot
Copy link
Collaborator

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

  • @Qiskit/terra-core

@1ucian0 1ucian0 added this to the 0.25.0 milestone Jul 24, 2023
@1ucian0 1ucian0 added the Changelog: Bugfix Include in the "Fixed" section of the changelog label Jul 24, 2023
Copy link
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.

Thanks for this Luciano. I quickly separated out the two failure paths to improve the error message and fixed the test (measure_all adds a new register), but this is good to go.

Aren't you meant to be on holiday?

@jakelishman jakelishman added the stable backport potential The bug might be minimal and/or import enough to be port to stable label Jul 24, 2023
@jakelishman jakelishman added this pull request to the merge queue Jul 24, 2023
Merged via the queue into Qiskit:main with commit 208c29a Jul 24, 2023
13 checks passed
mergify bot pushed a commit that referenced this pull request Jul 24, 2023
* raise an exception with a custom gate with no qubits or with one-or-more clbits

* QASM2ExportError

* test_circuit_raises_invalid_custom_gate_1

* test_circuit_raises_invalid_custom_gate_2

* reno

* Split error messages to be more specific

* Fix testing bug

* Fixup release note

---------

Co-authored-by: Jake Lishman <jake.lishman@ibm.com>
(cherry picked from commit 208c29a)
@omarcostahamido
Copy link
Contributor

thank you @1ucian0 @jakelishman

github-merge-queue bot pushed a commit that referenced this pull request Jul 24, 2023
… (#10485)

* raise an exception with a custom gate with no qubits or with one-or-more clbits

* QASM2ExportError

* test_circuit_raises_invalid_custom_gate_1

* test_circuit_raises_invalid_custom_gate_2

* reno

* Split error messages to be more specific

* Fix testing bug

* Fixup release note

---------

Co-authored-by: Jake Lishman <jake.lishman@ibm.com>
(cherry picked from commit 208c29a)

Co-authored-by: Luciano Bello <bel@zurich.ibm.com>
to24toro pushed a commit to to24toro/qiskit-terra that referenced this pull request Aug 3, 2023
…t#10438)

* raise an exception with a custom gate with no qubits or with one-or-more clbits

* QASM2ExportError

* test_circuit_raises_invalid_custom_gate_1

* test_circuit_raises_invalid_custom_gate_2

* reno

* Split error messages to be more specific

* Fix testing bug

* Fixup release note

---------

Co-authored-by: Jake Lishman <jake.lishman@ibm.com>
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 mod: qasm2 Relating to OpenQASM 2 import or export stable backport potential The bug might be minimal and/or import enough to be port to stable
Projects
None yet
5 participants