Skip to content

Commit

Permalink
Fix for issue #2777: it makes no sense to call simulator.run when ign…
Browse files Browse the repository at this point in the history
…ore_measurement_results has been set to True (#3618)

Raises a ValueError for DensityMatrixSimulator.run when ignore_measurement_results=True.

Fixes #2777.
  • Loading branch information
akushnarov committed Mar 17, 2021
1 parent aa6ec84 commit d27b9a4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
9 changes: 6 additions & 3 deletions cirq/sim/density_matrix_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,10 @@ def __init__(
In the other case:
>>> simulator = cirq.DensityMatrixSimulator(
... ignore_measurement_results = True)
>>> result = simulator.run(circuit)
The measurement result will be the maximally mixed state
with equal probability for 0 and 1.
will raise a `ValueError` exception if you call `simulator.run`
when `ignore_measurement_results` has been set to True
(for more see https://github.com/quantumlib/Cirq/issues/2777).
"""
if dtype not in {np.complex64, np.complex128}:
raise ValueError(f'dtype must be complex64 or complex128, was {dtype}')
Expand All @@ -160,6 +160,9 @@ def _run(
self, circuit: circuits.Circuit, param_resolver: study.ParamResolver, repetitions: int
) -> Dict[str, np.ndarray]:
"""See definition in `cirq.SimulatesSamples`."""
if self._ignore_measurement_results:
raise ValueError("run() is not supported when ignore_measurement_results = True")

param_resolver = param_resolver or study.ParamResolver({})
resolved_circuit = protocols.resolve_parameters(circuit, param_resolver)
check_all_resolved(resolved_circuit)
Expand Down
12 changes: 11 additions & 1 deletion cirq/sim/density_matrix_simulator_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ def test_invalid_dtype():
cirq.DensityMatrixSimulator(dtype=np.int32)


@pytest.mark.parametrize('dtype', [np.complex64, np.complex128])
def test_run_with_ignore_measurement_results(dtype):
q0, q1 = cirq.LineQubit.range(2)
simulator = cirq.DensityMatrixSimulator(dtype=dtype, ignore_measurement_results=True)

circuit = cirq.Circuit(cirq.X(q0), cirq.X(q1), cirq.measure(q0))
with pytest.raises(ValueError, match="ignore_measurement_results = True"):
simulator.run(circuit)


@pytest.mark.parametrize('dtype', [np.complex64, np.complex128])
def test_run_no_measurements(dtype):
q0, q1 = cirq.LineQubit.range(2)
Expand Down Expand Up @@ -109,7 +119,7 @@ def test_run_bit_flips(dtype):
@pytest.mark.parametrize('dtype', [np.complex64, np.complex128])
def test_run_bit_flips_with_dephasing(dtype):
q0, q1 = cirq.LineQubit.range(2)
simulator = cirq.DensityMatrixSimulator(dtype=dtype, ignore_measurement_results=True)
simulator = cirq.DensityMatrixSimulator(dtype=dtype)
for b0 in [0, 1]:
for b1 in [0, 1]:
circuit = cirq.Circuit(
Expand Down

0 comments on commit d27b9a4

Please sign in to comment.