-
Notifications
You must be signed in to change notification settings - Fork 27
fix: drop unroll-emitted global phase for QASM 2 targets #358
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -67,6 +67,113 @@ def test_whitelisted_ops(): | |
| check_unrolled_qasm(dumps(result), expected_qasm) | ||
|
|
||
|
|
||
| def test_rzz_unrolls_without_gphase(): | ||
| """Test that the global phase from the rzz decomposition is dropped for a QASM 2 | ||
| target, which has no global-phase syntax (issue #351)""" | ||
|
Comment on lines
+70
to
+72
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📐 Maintainability & Code Quality | 🟠 Major | ⚡ Quick win Add test function annotations and return documentation. Add As per coding guidelines, Also applies to: 98-100, 126-127, 146-148, 163-165 🤖 Prompt for AI AgentsSource: Coding guidelines |
||
| qasm2_string = """ | ||
| OPENQASM 2.0; | ||
| include 'qelib1.inc'; | ||
| qreg q[2]; | ||
| rzz(0.3) q[0], q[1]; | ||
| """ | ||
|
|
||
| expected_qasm = """ | ||
| OPENQASM 2.0; | ||
| include 'qelib1.inc'; | ||
| qreg q[2]; | ||
| cx q[0], q[1]; | ||
| rz(0.3) q[1]; | ||
| rx(1.5707963267948966) q[1]; | ||
| rz(3.141592653589793) q[1]; | ||
| rx(1.5707963267948966) q[1]; | ||
| rz(3.141592653589793) q[1]; | ||
| cx q[0], q[1]; | ||
| """ | ||
|
|
||
| result = loads(qasm2_string) | ||
| result.unroll() | ||
| check_unrolled_qasm(dumps(result), expected_qasm) | ||
|
|
||
|
|
||
| def test_rxx_unrolls_without_gphase(): | ||
| """Test that the global phase from the rxx decomposition is dropped for a QASM 2 | ||
| target (issue #351)""" | ||
| qasm2_string = """ | ||
| OPENQASM 2.0; | ||
| include 'qelib1.inc'; | ||
| qreg q[2]; | ||
| rxx(0.3) q[0], q[1]; | ||
| """ | ||
|
|
||
| expected_qasm = """ | ||
| OPENQASM 2.0; | ||
| include 'qelib1.inc'; | ||
| qreg q[2]; | ||
| h q[0]; | ||
| h q[1]; | ||
| cx q[0], q[1]; | ||
| rz(0.3) q[1]; | ||
| cx q[0], q[1]; | ||
| h q[1]; | ||
| h q[0]; | ||
| """ | ||
|
|
||
| result = loads(qasm2_string) | ||
| result.unroll() | ||
| check_unrolled_qasm(dumps(result), expected_qasm) | ||
|
|
||
|
|
||
| def test_conditional_rzz_unrolls_without_gphase(): | ||
| """Test that a conditional rzz body carries no gphase statement either (issue #351)""" | ||
| qasm2_string = """ | ||
| OPENQASM 2.0; | ||
| include 'qelib1.inc'; | ||
| qreg q[2]; | ||
| creg m[1]; | ||
| measure q[0] -> m[0]; | ||
| if(m==1) rzz(0.3) q[0], q[1]; | ||
| """ | ||
|
|
||
| result = loads(qasm2_string) | ||
| result.unroll() | ||
| unrolled = dumps(result) | ||
| assert "gphase" not in unrolled | ||
|
|
||
| # the unrolled output must be a loadable QASM 2 program | ||
| loads(unrolled).validate() | ||
|
|
||
|
|
||
| def test_unrolled_qasm2_round_trips(): | ||
| """Test that unrolled rzz output loads and re-unrolls cleanly: no gphase means the | ||
| second filtering pass has nothing to reject (issue #351)""" | ||
| qasm2_string = """ | ||
| OPENQASM 2.0; | ||
| include 'qelib1.inc'; | ||
| qreg q[2]; | ||
| rzz(0.3) q[0], q[1]; | ||
| """ | ||
|
|
||
| result = loads(qasm2_string) | ||
| result.unroll() | ||
| round_tripped = loads(dumps(result)) | ||
| round_tripped.unroll() | ||
| check_unrolled_qasm(dumps(round_tripped), dumps(result)) | ||
|
|
||
|
|
||
| def test_user_written_gphase_rejected(): | ||
| """Test that a gphase statement written in QASM 2 source is still rejected -- | ||
| OpenQASM 2 has no global-phase syntax, so only unroller-introduced phases are dropped""" | ||
| qasm2_string = """ | ||
| OPENQASM 2.0; | ||
| include 'qelib1.inc'; | ||
| qreg q[2]; | ||
| gphase(0.3); | ||
| """ | ||
|
|
||
| with pytest.raises(ValidationError): | ||
| loads(qasm2_string).validate() | ||
|
|
||
|
|
||
| def test_subroutine_blacklist(): | ||
|
|
||
| # subroutines | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📐 Maintainability & Code Quality | 🟠 Major | ⚡ Quick win
Add the test annotation and return documentation.
Add
-> None. Add aReturnssection that documents theNonereturn value.As per coding guidelines,
**/*.pyrequires type annotations for all functions and docstrings that explain return values.🤖 Prompt for AI Agents
Source: Coding guidelines