-
Notifications
You must be signed in to change notification settings - Fork 11
feature: support measure_ff and cc_prx gates #103
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4ad9062
feature: add cc_prx and measure_ff IQM classical control gates
rmshaffer 39f9602
fix: enable measure_ff and cc_prx on the AutoQASM simulator
rmshaffer f9ffd0d
Merge branch 'main' into rmshaffer/classical-control-gates
rmshaffer 85f57b5
Merge branch 'main' into rmshaffer/classical-control-gates
rmshaffer d2eead7
Merge branch 'main' into rmshaffer/classical-control-gates
rmshaffer 6dd9d28
ensure feedback keys are unique
rmshaffer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| # Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"). You | ||
| # may not use this file except in compliance with the License. A copy of | ||
| # the License is located at | ||
| # | ||
| # http://aws.amazon.com/apache2.0/ | ||
| # | ||
| # or in the "license" file accompanying this file. This file is | ||
| # distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF | ||
| # ANY KIND, either express or implied. See the License for the specific | ||
| # language governing permissions and limitations under the License. | ||
|
|
||
| """Tests for the IQM classical-control gates ``cc_prx`` and ``measure_ff``.""" | ||
|
|
||
| import math | ||
|
|
||
| import pytest | ||
| from braket.devices import LocalSimulator | ||
|
|
||
| import autoqasm as aq | ||
| from autoqasm.instructions import cc_prx, h, measure, measure_ff | ||
|
|
||
|
|
||
| def test_measure_ff_emits_feedback_key() -> None: | ||
| @aq.main | ||
| def program(): | ||
| h(0) | ||
| measure_ff(0, 0) | ||
|
|
||
| ir = program.build().to_ir() | ||
| assert "measure_ff(0) __qubits__[0];" in ir | ||
|
|
||
|
|
||
| def test_cc_prx_emits_angles_and_feedback_key() -> None: | ||
| @aq.main | ||
| def program(): | ||
| h(0) | ||
| measure_ff(0, 0) | ||
| cc_prx(1, 0.15, 0.25, 0) | ||
|
|
||
| ir = program.build().to_ir() | ||
| assert "cc_prx(0.15, 0.25, 0) __qubits__[1];" in ir | ||
|
|
||
|
|
||
| def test_measure_ff_different_feedback_keys() -> None: | ||
| @aq.main | ||
| def program(): | ||
| measure_ff(0, 0) | ||
| measure_ff(1, 5) | ||
|
|
||
| ir = program.build().to_ir() | ||
| assert "measure_ff(0) __qubits__[0];" in ir | ||
| assert "measure_ff(5) __qubits__[1];" in ir | ||
|
|
||
|
|
||
| def test_cc_prx_symbolic_angles() -> None: | ||
| """``cc_prx`` should accept ``FreeParameterExpression``-style angles | ||
| exactly like other parameterised gates (e.g. ``prx``).""" | ||
|
|
||
| @aq.main | ||
| def program(theta: float): | ||
| measure_ff(0, 0) | ||
| cc_prx(1, theta, 0.0, 0) | ||
|
|
||
| ir = program.build().to_ir() | ||
| assert "input float theta;" in ir | ||
| assert "cc_prx(theta, 0.0, 0) __qubits__[1];" in ir | ||
|
|
||
|
|
||
| def test_cc_prx_disallowed_inside_gate_definition() -> None: | ||
| """``cc_prx`` requires classical feedback; gate definitions must be | ||
| purely unitary. Using it inside ``@aq.gate`` should raise | ||
| ``InvalidGateDefinition``.""" | ||
|
|
||
| @aq.gate | ||
| def bad_gate(q: aq.Qubit): | ||
| cc_prx(q, 0.1, 0.2, 0) | ||
|
|
||
| @aq.main | ||
| def program(): | ||
| bad_gate(0) | ||
|
|
||
| with pytest.raises(aq.errors.InvalidGateDefinition): | ||
| program.build() | ||
|
|
||
|
|
||
| def test_measure_ff_disallowed_inside_gate_definition() -> None: | ||
| @aq.gate | ||
| def bad_gate(q: aq.Qubit): | ||
| measure_ff(q, 0) | ||
|
|
||
| @aq.main | ||
| def program(): | ||
| bad_gate(0) | ||
|
|
||
| with pytest.raises(aq.errors.InvalidGateDefinition): | ||
| program.build() | ||
|
|
||
|
|
||
| def test_classical_control_runs_on_local_simulator() -> None: | ||
| """End-to-end: measure |+> on qubit 0 (50/50 outcome), and conditionally | ||
| X qubit 1 via ``cc_prx(pi, 0, key)`` on the measured-1 branch. Qubit 1 | ||
| outcomes should track the qubit-0 feedback, giving ``00`` / ``11``.""" | ||
|
|
||
| @aq.main | ||
| def teleport_like(): | ||
| h(0) | ||
| measure_ff(0, 0) | ||
| cc_prx(1, math.pi, 0.0, 0) | ||
|
|
||
| result = LocalSimulator().run(teleport_like, shots=200).result() | ||
| counts = result.measurement_counts | ||
| # Outcomes should be only the correlated Bell-like pair. | ||
| for outcome in counts: | ||
| assert outcome in {"00", "11"}, f"unexpected outcome: {outcome}" | ||
| # With 200 shots we expect both outcomes to appear. | ||
| assert "00" in counts | ||
| assert "11" in counts | ||
|
|
||
|
|
||
| def test_classical_control_runs_on_autoqasm_simulator() -> None: | ||
| """Same behaviour on the AutoQASM-backed simulator.""" | ||
|
|
||
| @aq.main | ||
| def teleport_like(): | ||
| h(0) | ||
| measure_ff(0, 0) | ||
| cc_prx(1, math.pi, 0.0, 0) | ||
| measure(1) | ||
|
|
||
| result = LocalSimulator("autoqasm").run(teleport_like, shots=100).result() | ||
| measurements = result.measurements | ||
| feedback = [bool(v) for v in measurements["__ff_0__"]] | ||
| qubit_1_key = next(k for k in measurements if k.startswith("__bit_")) | ||
| qubit_1 = [bool(v) for v in measurements[qubit_1_key]] | ||
| # Qubit 1 should match the feedback bit every time. | ||
| assert feedback == qubit_1, "cc_prx failed to conditionally flip qubit 1" | ||
| # Both outcomes should appear with 100 shots. | ||
| assert any(feedback) | ||
| assert not all(feedback) | ||
|
|
||
|
|
||
| def test_cc_prx_missing_feedback_raises() -> None: | ||
| """If ``cc_prx`` is used before any ``measure_ff`` with the same | ||
| feedback key, the AutoQASM simulator raises a clean ValueError.""" | ||
|
|
||
| @aq.main | ||
| def missing_key(): | ||
| cc_prx(0, 0.1, 0.2, 42) | ||
|
|
||
| with pytest.raises(ValueError, match="feedback key 42"): | ||
| LocalSimulator("autoqasm").run(missing_key, shots=1).result() | ||
|
|
||
|
|
||
| def test_measure_ff_duplicate_feedback_key_raises() -> None: | ||
| """IQM requires feedback keys to be unique within a program; the | ||
| AutoQASM simulator should raise ``ValueError`` when a feedback key | ||
| is reused within a single shot.""" | ||
|
|
||
| @aq.main | ||
| def duplicate_key(): | ||
| measure_ff(0, 7) | ||
| measure_ff(1, 7) | ||
|
|
||
| with pytest.raises(ValueError, match="feedback key 7"): | ||
| LocalSimulator("autoqasm").run(duplicate_key, shots=1).result() | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.