-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Ensure simulator runs circuit compatible with a provided noise model's properties #8139
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
Open
Acciaccatura
wants to merge
12
commits into
quantumlib:main
Choose a base branch
from
Acciaccatura:issue6608
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+83
−1
Open
Changes from 9 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
2898e9a
bug: added gate/qubit compatability checking for simulations
Acciaccatura cd2742f
Merge branch 'quantumlib:main' into issue6608
Acciaccatura af89fda
feat: reformat with ruff
Acciaccatura 7cc1a2b
Merge branch 'issue6608' of https://github.com/Acciaccatura/Cirq into…
Acciaccatura e671019
bug: fix missed code path due to mistype
Acciaccatura 57e7e91
lint: fix some lint issues from ruff
Acciaccatura 0a6f08e
bug: add multiple checks to test each simulation case
Acciaccatura 2cc7287
lint: remove trailing whitespace
Acciaccatura 2186487
bug: add value to obey typechecking
Acciaccatura 2ebef32
lint: undo lint changes from ruff
Acciaccatura 6d75ec0
bug: mistype
Acciaccatura 10173fc
Merge branch 'quantumlib:main' into issue6608
Acciaccatura 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,8 @@ | |
| import sympy | ||
|
|
||
| import cirq | ||
| from cirq import devices, ops | ||
| from cirq.devices import noise_utils | ||
|
|
||
|
|
||
| class CountingState(cirq.qis.QuantumStateRepresentation): | ||
|
|
@@ -229,7 +231,7 @@ def test_parameterized_copies_all_but_last() -> None: | |
| sim = CountingSimulator() | ||
| n = 4 | ||
| rs = sim.simulate_sweep( | ||
| cirq.Circuit(cirq.X(q0) ** sympy.Symbol('a')), [{'a': i} for i in range(n)] | ||
| cirq.Circuit(cirq.X(q0) ** sympy.Symbol("a")), [{"a": i} for i in range(n)] | ||
| ) | ||
| for i in range(n): | ||
| r = rs[i] | ||
|
|
@@ -252,19 +254,19 @@ def _act_on_(self, sim_state): | |
| def test_run_one_gate_circuit() -> None: | ||
| sim = CountingSimulator() | ||
| r = sim.run(cirq.Circuit(cirq.X(q0), cirq.measure(q0)), repetitions=2) | ||
| assert np.allclose(r.measurements['q(0)'], [[1], [1]]) | ||
| assert np.allclose(r.measurements["q(0)"], [[1], [1]]) | ||
|
|
||
|
|
||
| def test_run_one_gate_circuit_noise() -> None: | ||
| sim = CountingSimulator(noise=cirq.X) | ||
| r = sim.run(cirq.Circuit(cirq.X(q0), cirq.measure(q0)), repetitions=2) | ||
| assert np.allclose(r.measurements['q(0)'], [[2], [2]]) | ||
| assert np.allclose(r.measurements["q(0)"], [[2], [2]]) | ||
|
|
||
|
|
||
| def test_run_non_unitary_circuit() -> None: | ||
| sim = CountingSimulator() | ||
| r = sim.run(cirq.Circuit(cirq.phase_damp(1).on(q0), cirq.measure(q0)), repetitions=2) | ||
| assert np.allclose(r.measurements['q(0)'], [[1], [1]]) | ||
| assert np.allclose(r.measurements["q(0)"], [[1], [1]]) | ||
|
|
||
|
|
||
| def test_run_non_unitary_circuit_non_unitary_state() -> None: | ||
|
|
@@ -274,13 +276,13 @@ def _can_be_in_run_prefix(self, val): | |
|
|
||
| sim = DensityCountingSimulator() | ||
| r = sim.run(cirq.Circuit(cirq.phase_damp(1).on(q0), cirq.measure(q0)), repetitions=2) | ||
| assert np.allclose(r.measurements['q(0)'], [[1], [1]]) | ||
| assert np.allclose(r.measurements["q(0)"], [[1], [1]]) | ||
|
|
||
|
|
||
| def test_run_non_terminal_measurement() -> None: | ||
| sim = CountingSimulator() | ||
| r = sim.run(cirq.Circuit(cirq.X(q0), cirq.measure(q0), cirq.X(q0)), repetitions=2) | ||
| assert np.allclose(r.measurements['q(0)'], [[1], [1]]) | ||
| assert np.allclose(r.measurements["q(0)"], [[1], [1]]) | ||
|
|
||
|
|
||
| def test_integer_initial_state_is_split() -> None: | ||
|
|
@@ -370,7 +372,7 @@ def test_reorder_succeeds() -> None: | |
| assert reordered.qubits == (q1, q0) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize('split', [True, False]) | ||
| @pytest.mark.parametrize("split", [True, False]) | ||
| def test_sim_state_instance_unchanged_during_normal_sim(split: bool) -> None: | ||
| sim = SplittableCountingSimulator(split_untangled_states=split) | ||
| state = sim._create_simulation_state(0, (q0, q1)) | ||
|
|
@@ -383,12 +385,12 @@ def test_sim_state_instance_unchanged_during_normal_sim(split: bool) -> None: | |
| def test_measurements_retained_in_step_results() -> None: | ||
| sim = SplittableCountingSimulator() | ||
| circuit = cirq.Circuit( | ||
| cirq.measure(q0, key='a'), cirq.measure(q0, key='b'), cirq.measure(q0, key='c') | ||
| cirq.measure(q0, key="a"), cirq.measure(q0, key="b"), cirq.measure(q0, key="c") | ||
| ) | ||
| iterator = sim.simulate_moment_steps(circuit) | ||
| assert next(iterator).measurements.keys() == {'a'} | ||
| assert next(iterator).measurements.keys() == {'a', 'b'} | ||
| assert next(iterator).measurements.keys() == {'a', 'b', 'c'} | ||
| assert next(iterator).measurements.keys() == {"a"} | ||
| assert next(iterator).measurements.keys() == {"a", "b"} | ||
| assert next(iterator).measurements.keys() == {"a", "b", "c"} | ||
| assert not any(iterator) | ||
|
|
||
|
|
||
|
|
@@ -415,11 +417,11 @@ def _has_unitary_(self): | |
| return self.has_unitary | ||
|
|
||
| simulator = CountingSimulator() | ||
| params = [cirq.ParamResolver({'a': 0}), cirq.ParamResolver({'a': 1})] | ||
| params = [cirq.ParamResolver({"a": 0}), cirq.ParamResolver({"a": 1})] | ||
|
|
||
| op1 = TestOp(has_unitary=True) | ||
| op2 = TestOp(has_unitary=True) | ||
| circuit = cirq.Circuit(op1, cirq.XPowGate(exponent=sympy.Symbol('a'))(q), op2) | ||
| circuit = cirq.Circuit(op1, cirq.XPowGate(exponent=sympy.Symbol("a"))(q), op2) | ||
| rs = simulator.simulate_sweep(program=circuit, params=params) | ||
| assert isinstance(rs[0]._final_simulator_state, CountingSimulationState) | ||
| assert isinstance(rs[1]._final_simulator_state, CountingSimulationState) | ||
|
|
@@ -430,7 +432,7 @@ def _has_unitary_(self): | |
|
|
||
| op1 = TestOp(has_unitary=False) | ||
| op2 = TestOp(has_unitary=False) | ||
| circuit = cirq.Circuit(op1, cirq.XPowGate(exponent=sympy.Symbol('a'))(q), op2) | ||
| circuit = cirq.Circuit(op1, cirq.XPowGate(exponent=sympy.Symbol("a"))(q), op2) | ||
| rs = simulator.simulate_sweep(program=circuit, params=params) | ||
| assert isinstance(rs[0]._final_simulator_state, CountingSimulationState) | ||
| assert isinstance(rs[1]._final_simulator_state, CountingSimulationState) | ||
|
|
@@ -442,7 +444,7 @@ def _has_unitary_(self): | |
|
|
||
| def test_inhomogeneous_measurement_count_padding() -> None: | ||
| q = cirq.LineQubit(0) | ||
| key = cirq.MeasurementKey('m') | ||
| key = cirq.MeasurementKey("m") | ||
| sim = cirq.Simulator() | ||
| c = cirq.Circuit( | ||
| cirq.CircuitOperation( | ||
|
|
@@ -453,4 +455,51 @@ def test_inhomogeneous_measurement_count_padding() -> None: | |
| ) | ||
| results = sim.run(c, repetitions=10) | ||
| for i in range(10): | ||
| assert np.sum(results.records['m'][i, :, :]) == 1 | ||
| assert np.sum(results.records["m"][i, :, :]) == 1 | ||
|
|
||
|
|
||
| def test_simulates_noise_only_on_valid_gates_and_qubits() -> None: | ||
| expected_single_qubit_gates = [cirq.XPowGate, cirq.ZPowGate] | ||
| expected_qubits = [cirq.GridQubit(1, 2)] | ||
|
|
||
| unexpected_qubits = [cirq.GridQubit(2, 2)] | ||
|
|
||
| class TestNoiseProperties(devices.SuperconductingQubitsNoiseProperties): | ||
|
Contributor
Author
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. the |
||
| @classmethod | ||
| def single_qubit_gates(cls) -> set[type[ops.Gate]]: | ||
| return set(expected_single_qubit_gates) | ||
|
|
||
| @classmethod | ||
| def symmetric_two_qubit_gates(cls) -> set[type[ops.Gate]]: | ||
| return set() | ||
|
|
||
| @classmethod | ||
| def asymmetric_two_qubit_gates(cls) -> set[type[ops.Gate]]: | ||
| return set() | ||
|
|
||
| noise_props = TestNoiseProperties( | ||
| gate_times_ns=dict.fromkeys(expected_single_qubit_gates, 1e9), | ||
| t1_ns=dict.fromkeys(expected_qubits, 1e9), | ||
| tphi_ns=dict.fromkeys(expected_qubits, 1e9), | ||
| readout_errors=dict.fromkeys(expected_qubits, [0.5, 0.5]), | ||
| gate_pauli_errors={ | ||
| noise_utils.OpIdentifier(gate, expected_qubits[0]): 0 | ||
| for gate in expected_single_qubit_gates | ||
| }, | ||
| ) | ||
| noise_model = devices.NoiseModelFromNoiseProperties(noise_props) | ||
|
|
||
| simulator = cirq.Simulator(noise=noise_model) | ||
|
|
||
| valid_circuit_invalid_qubits = cirq.Circuit(cirq.X(unexpected_qubits[0])) | ||
| valid_circuit_valid_qubits = cirq.Circuit(cirq.X(expected_qubits[0])) | ||
| invalid_circuit_invalid_qubits = cirq.Circuit(cirq.Y(unexpected_qubits[0])) | ||
| invalid_circuit_valid_qubits = cirq.Circuit(cirq.Y(expected_qubits[0])) | ||
|
|
||
| with pytest.raises(TypeError): | ||
| simulator.simulate(invalid_circuit_invalid_qubits) | ||
| with pytest.raises(TypeError): | ||
| simulator.simulate(invalid_circuit_valid_qubits) | ||
| with pytest.raises(TypeError): | ||
| simulator.simulate(valid_circuit_invalid_qubits) | ||
| simulator.simulate(valid_circuit_valid_qubits) | ||
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.
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.
lots of modifications to this file due to running
ruff format.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.
Please revert. Use
check/format-incrementalfor formatting instead.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.
sure, will do - fixed!