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

Split the simulators' creation of ActOnArgs and iteration #3970

Merged
merged 52 commits into from
Apr 20, 2021
Merged
Changes from 1 commit
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
8ad4224
Move MPSSimulator to use act_on
tonybruguier Mar 20, 2021
ffd7b86
Add axes to mps
daxfohl Mar 23, 2021
6a28c01
Finish act-on for mps
daxfohl Mar 23, 2021
ca0a765
lint
daxfohl Mar 23, 2021
988d7ae
Add a copy method for all act_on_args
daxfohl Mar 23, 2021
b1774b7
Split creation of act_on_args and iteration
daxfohl Mar 24, 2021
bfa7ed0
remove the empty yields
daxfohl Mar 24, 2021
180bb67
Fix most unit tests
daxfohl Mar 24, 2021
25efdad
Change sparse simulator to use act_on_args in _run.
daxfohl Mar 24, 2021
49f4ae4
Fix clifford simulator
daxfohl Mar 24, 2021
d11d34f
format
daxfohl Mar 24, 2021
0ee6526
optimize imports
daxfohl Mar 24, 2021
f7f792a
Address PR comments
daxfohl Mar 25, 2021
11ac1d2
Merge branch 'master' into mps
daxfohl Mar 25, 2021
3f6d0e6
Unit test
daxfohl Mar 25, 2021
6c91dd8
Formatting
daxfohl Mar 25, 2021
39117a7
Merge branch 'mps' into spliterate
daxfohl Mar 25, 2021
1a5fea4
Merge branch 'master' into spliterate
daxfohl Mar 25, 2021
0c8763e
Rename, format
daxfohl Mar 25, 2021
ccc294c
Fix docs
daxfohl Mar 25, 2021
0edf566
Update quantum teleportation
daxfohl Mar 25, 2021
31fc327
Update quantum teleportation
daxfohl Mar 25, 2021
838d103
Update quantum teleportation
daxfohl Mar 25, 2021
f664e9d
Make the QubitOrder arg optional
daxfohl Mar 25, 2021
5a35ca9
Fix the engine_simulator.py overrides
daxfohl Mar 25, 2021
2d0ee42
Fix the engine_simulator.py overrides
daxfohl Mar 25, 2021
22b3b92
Fix the engine_simulator.py overrides
daxfohl Mar 25, 2021
5b98a6b
Fix coverage check
daxfohl Mar 25, 2021
f970a38
Change quantum teleportation example to be generic across simulators.
daxfohl Mar 28, 2021
1fd8e8c
Change quantum teleportation example to be generic across simulators.
daxfohl Mar 28, 2021
45cfdba
Change qubit_order to an explicit ordering of qubits.
daxfohl Mar 28, 2021
b34ac3a
Clean PR issues
daxfohl Apr 3, 2021
c9f12db
Merge branch 'master' into spliterate
daxfohl Apr 3, 2021
80b4202
Fix type check
daxfohl Apr 3, 2021
645988e
Fix unit test
daxfohl Apr 3, 2021
3ea6c69
Spelling
daxfohl Apr 4, 2021
2102af3
small changes
daxfohl Apr 5, 2021
637d88b
Merge remote-tracking branch 'origin/spliterate' into spliterate
daxfohl Apr 5, 2021
452a950
test change
daxfohl Apr 5, 2021
d1a42ad
make _core_iterator.sim_state naming consistent
daxfohl Apr 10, 2021
6465a23
Allow create_act_on_args to accept an ActOnArgs parameter, and return…
daxfohl Apr 10, 2021
814e82a
Merge branch 'master' into spliterate
daxfohl Apr 11, 2021
2fe1e8a
Add tests for `.simulate(ActOnArgs)`.
daxfohl Apr 11, 2021
cfb711e
Add tests for `.simulate(ActOnArgs)`.
daxfohl Apr 11, 2021
e2063df
lint
daxfohl Apr 11, 2021
8913bda
Plop qubits and qubit_map into ActOnArgs instead of passing together …
daxfohl Apr 11, 2021
d195016
Change qubits param to Sequence. Clean up teleportation example.
daxfohl Apr 12, 2021
739d69e
Merge branch 'master' into spliterate
daxfohl Apr 15, 2021
5ad813c
PR comments:
daxfohl Apr 19, 2021
f319a22
format/lint
daxfohl Apr 19, 2021
e5ff9fd
Merge branch 'master' into spliterate
daxfohl Apr 20, 2021
0e7ee06
format
daxfohl Apr 20, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Change sparse simulator to use act_on_args in _run.
daxfohl committed Mar 24, 2021
commit 25efdad0e267c66cdde599884b82fc19ae811665
2 changes: 1 addition & 1 deletion cirq/sim/clifford/clifford_simulator.py
Original file line number Diff line number Diff line change
@@ -184,7 +184,7 @@ def __str__(self) -> str:
class CliffordSimulatorStepResult(simulator.StepResult['CliffordState']):
"""A `StepResult` that includes `StateVectorMixin` methods."""

def __init__(self, state, measurements):
def __init__(self, state: 'CliffordState', measurements):
"""Results of a step of the simulator.
Attributes:
state: A CliffordState
5 changes: 2 additions & 3 deletions cirq/sim/density_matrix_simulator.py
Original file line number Diff line number Diff line change
@@ -181,14 +181,13 @@ def _run(
pass
assert step_result is not None

intermediate_state = acton_args.copy()
if general_suffix.are_all_measurements_terminal() and not any(
general_suffix.findall_operations(lambda op: isinstance(op, circuits.CircuitOperation))
):
return self._run_sweep_sample(
general_suffix, repetitions, qubit_order, intermediate_state
general_suffix, repetitions, qubit_order, acton_args
)
return self._run_sweep_repeat(general_suffix, repetitions, qubit_order, intermediate_state)
return self._run_sweep_repeat(general_suffix, repetitions, qubit_order, acton_args)

def _run_sweep_sample(
self,
15 changes: 7 additions & 8 deletions cirq/sim/sparse_simulator.py
Original file line number Diff line number Diff line change
@@ -182,11 +182,12 @@ def _run(
if protocols.has_unitary(self.noise)
else (resolved_circuit[0:0], resolved_circuit)
)
acton_args = self.create_act_on_args(unitary_prefix, qubit_order, 0)
step_result = None
for step_result in self._base_iterator(
for step_result in self.iterate_circuit(
circuit=unitary_prefix,
qubit_order=qubit_order,
initial_state=0,
sim_state=acton_args,
):
pass
assert step_result is not None
@@ -201,18 +202,16 @@ def _run(
seed=self._prng,
)

qid_shape = protocols.qid_shape(qubit_order)
intermediate_state = step_result.state_vector().reshape(qid_shape)
return self._brute_force_samples(
initial_state=intermediate_state,
acton_args=acton_args,
circuit=general_suffix,
repetitions=repetitions,
qubit_order=qubit_order,
)

def _brute_force_samples(
self,
initial_state: np.ndarray,
acton_args: act_on_state_vector_args.ActOnStateVectorArgs,
circuit: circuits.Circuit,
qubit_order: 'cirq.QubitOrderOrList',
repetitions: int,
@@ -221,8 +220,8 @@ def _brute_force_samples(

measurements: DefaultDict[str, List[np.ndarray]] = collections.defaultdict(list)
for _ in range(repetitions):
all_step_results = self._base_iterator(
circuit, initial_state=initial_state, qubit_order=qubit_order
all_step_results = self.iterate_circuit(
circuit, sim_state=acton_args.copy(), qubit_order=qubit_order
)

for step_result in all_step_results:
12 changes: 6 additions & 6 deletions cirq/sim/sparse_simulator_test.py
Original file line number Diff line number Diff line change
@@ -89,7 +89,7 @@ def test_run_bit_flips(dtype):
def test_run_measure_at_end_no_repetitions(dtype):
q0, q1 = cirq.LineQubit.range(2)
simulator = cirq.Simulator(dtype=dtype)
with mock.patch.object(simulator, '_base_iterator', wraps=simulator._base_iterator) as mock_sim:
with mock.patch.object(simulator, 'iterate_circuit', wraps=simulator.iterate_circuit) as mock_sim:
for b0 in [0, 1]:
for b1 in [0, 1]:
circuit = cirq.Circuit(
@@ -114,7 +114,7 @@ def test_run_repetitions_terminal_measurement_stochastic():
def test_run_repetitions_measure_at_end(dtype):
q0, q1 = cirq.LineQubit.range(2)
simulator = cirq.Simulator(dtype=dtype)
with mock.patch.object(simulator, '_base_iterator', wraps=simulator._base_iterator) as mock_sim:
with mock.patch.object(simulator, 'iterate_circuit', wraps=simulator.iterate_circuit) as mock_sim:
for b0 in [0, 1]:
for b1 in [0, 1]:
circuit = cirq.Circuit(
@@ -131,7 +131,7 @@ def test_run_repetitions_measure_at_end(dtype):
def test_run_invert_mask_measure_not_terminal(dtype):
q0, q1 = cirq.LineQubit.range(2)
simulator = cirq.Simulator(dtype=dtype)
with mock.patch.object(simulator, '_base_iterator', wraps=simulator._base_iterator) as mock_sim:
with mock.patch.object(simulator, 'iterate_circuit', wraps=simulator.iterate_circuit) as mock_sim:
for b0 in [0, 1]:
for b1 in [0, 1]:
circuit = cirq.Circuit(
@@ -151,7 +151,7 @@ def test_run_invert_mask_measure_not_terminal(dtype):
def test_run_partial_invert_mask_measure_not_terminal(dtype):
q0, q1 = cirq.LineQubit.range(2)
simulator = cirq.Simulator(dtype=dtype)
with mock.patch.object(simulator, '_base_iterator', wraps=simulator._base_iterator) as mock_sim:
with mock.patch.object(simulator, 'iterate_circuit', wraps=simulator.iterate_circuit) as mock_sim:
for b0 in [0, 1]:
for b1 in [0, 1]:
circuit = cirq.Circuit(
@@ -171,7 +171,7 @@ def test_run_partial_invert_mask_measure_not_terminal(dtype):
def test_run_measurement_not_terminal_no_repetitions(dtype):
q0, q1 = cirq.LineQubit.range(2)
simulator = cirq.Simulator(dtype=dtype)
with mock.patch.object(simulator, '_base_iterator', wraps=simulator._base_iterator) as mock_sim:
with mock.patch.object(simulator, 'iterate_circuit', wraps=simulator.iterate_circuit) as mock_sim:
for b0 in [0, 1]:
for b1 in [0, 1]:
circuit = cirq.Circuit(
@@ -194,7 +194,7 @@ def test_run_measurement_not_terminal_no_repetitions(dtype):
def test_run_repetitions_measurement_not_terminal(dtype):
q0, q1 = cirq.LineQubit.range(2)
simulator = cirq.Simulator(dtype=dtype)
with mock.patch.object(simulator, '_base_iterator', wraps=simulator._base_iterator) as mock_sim:
with mock.patch.object(simulator, 'iterate_circuit', wraps=simulator.iterate_circuit) as mock_sim:
for b0 in [0, 1]:
for b1 in [0, 1]:
circuit = cirq.Circuit(