diff --git a/qsimcirq/qsim_circuit.py b/qsimcirq/qsim_circuit.py index 0da46be0..9d33881e 100644 --- a/qsimcirq/qsim_circuit.py +++ b/qsimcirq/qsim_circuit.py @@ -343,7 +343,6 @@ def __init__( cirq_circuit: cirq.Circuit, allow_decomposition: bool = False, ): - if allow_decomposition: super().__init__() for moment in cirq_circuit: @@ -352,6 +351,7 @@ def __init__( self.append(op) else: super().__init__(cirq_circuit) + self._check_for_confusion_matrix() def __eq__(self, other): if not isinstance(other, QSimCircuit): @@ -364,6 +364,24 @@ def _resolve_parameters_( ): return QSimCircuit(cirq.resolve_parameters(super(), param_resolver, recursive)) + def _check_for_confusion_matrix(self): + """Checks cirq Circuit for Measurement Gates with confusion matrices. + Returns: + Throws a runtime exception if a MeasurementGate with a confusion matrix is included in the circuit + """ + confusion_maps_on_measurement_gates = [ + op.gate.confusion_map + for _, op, _ in self.findall_operations_with_gate_type(cirq.MeasurementGate) + if op.gate.confusion_map + ] + for confusion_map in confusion_maps_on_measurement_gates: + for map_values in confusion_map.values(): + if map_values: + raise ValueError( + "Confusion Matrices are not currently supported in Qsim. " + "See https://github.com/quantumlib/Cirq/issues/6305 for latest status" + ) + def translate_cirq_to_qsim( self, qubit_order: cirq.QubitOrderOrList = cirq.QubitOrder.DEFAULT ) -> qsim.Circuit: diff --git a/qsimcirq/qsim_simulator.py b/qsimcirq/qsim_simulator.py index 1bf0a4d0..2513ba50 100644 --- a/qsimcirq/qsim_simulator.py +++ b/qsimcirq/qsim_simulator.py @@ -213,7 +213,7 @@ def _run( """Run a simulation, mimicking quantum hardware. Args: - program: The circuit to simulate. + circuit: The circuit to simulate. param_resolver: Parameters to run with the program. repetitions: Number of times to repeat the run. diff --git a/qsimcirq_tests/qsimcirq_test.py b/qsimcirq_tests/qsimcirq_test.py index e0d19fae..40d0c261 100644 --- a/qsimcirq_tests/qsimcirq_test.py +++ b/qsimcirq_tests/qsimcirq_test.py @@ -336,6 +336,19 @@ def test_numpy_params(): qsim_result = qsim_simulator.simulate_sweep(circuit, params=prs) +def test_confusion_matrix_exception(): + qubit = cirq.LineQubit(0) + cmap = {(0,): np.array([[0.8, 0.2], [0.2, 0.8]])} + circuit = cirq.Circuit() + circuit += cirq.X(qubit) + circuit += cirq.MeasurementGate(1, confusion_map=cmap)(qubit) + x, y = sympy.Symbol("x"), sympy.Symbol("y") + prs = [{x: np.int64(0), y: np.int64(1)}] + qsim_simulator = qsimcirq.QSimSimulator() + with pytest.raises(ValueError): + _ = qsim_simulator.simulate_sweep(circuit, params=prs) + + def test_invalid_params(): # Parameters must have numeric values. q0 = cirq.LineQubit(0) @@ -1219,7 +1232,6 @@ def test_multi_qubit_fusion(): @pytest.mark.parametrize("mode", ["noiseless", "noisy"]) def test_cirq_qsim_simulate_random_unitary(mode: str): - q0, q1 = cirq.LineQubit.range(2) options = qsimcirq.QSimOptions(cpu_threads=16, verbosity=0) qsimSim = qsimcirq.QSimSimulator(qsim_options=options)