Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
jakurzak authored Nov 9, 2023
2 parents e09e6b7 + 3e7da1b commit 33c49f2
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
20 changes: 19 additions & 1 deletion qsimcirq/qsim_circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,6 @@ def __init__(
cirq_circuit: cirq.Circuit,
allow_decomposition: bool = False,
):

if allow_decomposition:
super().__init__()
for moment in cirq_circuit:
Expand All @@ -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):
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion qsimcirq/qsim_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
14 changes: 13 additions & 1 deletion qsimcirq_tests/qsimcirq_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 33c49f2

Please sign in to comment.