def test_parallel_gate_family_eq(): eq = cirq.testing.EqualsTester() for name, description in [(None, None), ("Custom Name", "Custom Description")]: eq.add_equality_group( cirq.ParallelGateFamily(CustomX, max_parallel_allowed=2, name=name, description=description), cirq.ParallelGateFamily(cirq.ParallelGate(CustomX, 2), name=name, description=description), ) eq.add_equality_group( cirq.ParallelGateFamily(CustomXPowGate, max_parallel_allowed=2, name=name, description=description)) eq.add_equality_group( cirq.ParallelGateFamily(CustomX, max_parallel_allowed=5, name=name, description=description), cirq.ParallelGateFamily( cirq.ParallelGate(CustomX, 10), max_parallel_allowed=5, name=name, description=description, ), )
def test_extrapolate(): # If the gate isn't extrapolatable, you get a type error. g = cirq.ParallelGate(cirq.testing.SingleQubitGate(), 2) with pytest.raises(TypeError): _ = g**0.5 # If the gate is extrapolatable, the effect is applied on the underlying gate. g = cirq.ParallelGate(cirq.Y, 2) assert g**0.5 == cirq.ParallelGate(cirq.Y**0.5, 2) assert cirq.inverse(g) == g**-1 == cirq.ParallelGate(cirq.Y**-1, 2)
def test_trace_distance(): s = cirq.X**0.25 two_g = cirq.ParallelGate(s, 2) three_g = cirq.ParallelGate(s, 3) four_g = cirq.ParallelGate(s, 4) assert cirq.approx_eq(cirq.trace_distance_bound(two_g), np.sin(np.pi / 4)) assert cirq.approx_eq(cirq.trace_distance_bound(three_g), np.sin(3 * np.pi / 8)) assert cirq.approx_eq(cirq.trace_distance_bound(four_g), 1.0) spg = cirq.ParallelGate(cirq.X**sympy.Symbol('a'), 4) assert cirq.approx_eq(cirq.trace_distance_bound(spg), 1.0)
def test_validate_operation_errors(): d = square_device(3, 3) class bad_op(cirq.Operation): def bad_op(self): pass def qubits(self): pass def with_qubits(self, new_qubits): pass with pytest.raises(ValueError, match="Unsupported operation"): d.validate_operation(bad_op()) not_on_device_op = cirq.parallel_gate_op( cirq.X, *[cirq.GridQubit(row, col) for col in range(4) for row in range(4)] ) with pytest.raises(ValueError, match="Qubit not on device"): d.validate_operation(not_on_device_op) with pytest.raises(ValueError, match="Too many qubits acted on in parallel by"): d.validate_operation(cirq.CCX.on(*d.qubit_list()[0:3])) with pytest.raises(ValueError, match="are too far away"): d.validate_operation(cirq.CZ.on(cirq.GridQubit(0, 0), cirq.GridQubit(2, 2))) with pytest.raises(ValueError, match="Unsupported operation"): d.validate_operation(cirq.parallel_gate_op(cirq.Z, *d.qubits)) with pytest.raises(ValueError, match="Unsupported operation"): d.validate_operation(cirq.parallel_gate_op(cirq.X, *d.qubit_list()[1:])) with pytest.raises(ValueError, match="Unsupported operation"): d.validate_operation( cirq.ParallelGate(cirq.MeasurementGate(1, key='a'), 4)(*d.qubit_list()[:4]) )
def test_coverage(): q = cirq.LineQubit.range(3) g = cirq.testing.ThreeQubitGate() class FakeOperation(ops.Operation): def __init__(self, gate, qubits): self._gate = gate self._qubits = qubits @property def qubits(self): return self._qubits def with_qubits(self, *new_qubits): return FakeOperation(self._gate, new_qubits) op = FakeOperation(g, q).with_qubits(*q) circuit_ops = [cirq.Y(q[0]), cirq.ParallelGate(cirq.X, 3).on(*q)] c = cirq.Circuit(circuit_ops) cirq.neutral_atoms.ConvertToNeutralAtomGates().optimize_circuit(c) assert c == cirq.Circuit(circuit_ops) assert cirq.neutral_atoms.ConvertToNeutralAtomGates().convert(cirq.X.on(q[0])) == [ cirq.X.on(q[0]) ] with pytest.raises(TypeError, match="Don't know how to work with"): cirq.neutral_atoms.ConvertToNeutralAtomGates().convert(op) assert not cirq.neutral_atoms.is_native_neutral_atom_op(op) assert not cirq.neutral_atoms.is_native_neutral_atom_gate(g)
def test_unitary(gate, num_copies, qubits): g = cirq.ParallelGate(gate, num_copies) step = gate.num_qubits() qubit_lists = [qubits[i * step:(i + 1) * step] for i in range(num_copies)] np.testing.assert_allclose(cirq.unitary(g), cirq.unitary( cirq.Circuit(gate.on_each(qubit_lists))), atol=1e-8)
def test_parallel_gate_family(gate, name, description, max_parallel_allowed): gate_family = cirq.ParallelGateFamily( gate, name=name, description=description, max_parallel_allowed=max_parallel_allowed ) cirq.testing.assert_equivalent_repr(gate_family) for gate_to_test in [CustomX, cirq.ParallelGate(CustomX, 2)]: assert gate_to_test in gate_family assert gate_to_test(*cirq.LineQubit.range(cirq.num_qubits(gate_to_test))) in gate_family if isinstance(gate, cirq.ParallelGate) and not max_parallel_allowed: assert gate_family._max_parallel_allowed == cirq.num_qubits(gate) assert cirq.ParallelGate(CustomX, 4) not in gate_family else: assert gate_family._max_parallel_allowed == max_parallel_allowed assert (cirq.ParallelGate(CustomX, 4) in gate_family) == (max_parallel_allowed is None) str_to_search = 'Custom' if name else 'Parallel' assert str_to_search in gate_family.name assert str_to_search in gate_family.description
def test_equivalent_circuit(): qreg = cirq.LineQubit.range(4) oldc = cirq.Circuit() newc = cirq.Circuit() single_qubit_gates = [cirq.X**(1 / 2), cirq.Y**(1 / 3), cirq.Z**-1] for gate in single_qubit_gates: for qubit in qreg: oldc.append(gate.on(qubit)) newc.append(cirq.ParallelGate(gate, 4)(*qreg)) cirq.testing.assert_has_diagram(newc, oldc.to_text_diagram()) cirq.testing.assert_circuits_with_terminal_measurements_are_equivalent( oldc, newc, atol=1e-6)
def test_parameterizable_gates(resolve_fn): r = cirq.ParamResolver({'a': 0.5}) g1 = cirq.ParallelGate(cirq.Z**sympy.Symbol('a'), 2) assert cirq.is_parameterized(g1) g2 = resolve_fn(g1, r) assert not cirq.is_parameterized(g2)
def test_with_num_copies(): g = cirq.testing.SingleQubitGate() pg = cirq.ParallelGate(g, 3) assert pg.with_num_copies(5) == cirq.ParallelGate(g, 5)
def test_decompose_raises(): g = cirq.ParallelGate(cirq.X, 2) qubits = cirq.LineQubit.range(4) with pytest.raises(ValueError, match=r'len\(qubits\)=4 should be 2'): cirq.decompose_once_with_qubits(g, qubits)
def test_decompose(gate, num_copies, qubits): g = cirq.ParallelGate(gate, num_copies) step = gate.num_qubits() qubit_lists = [qubits[i * step:(i + 1) * step] for i in range(num_copies)] assert set(cirq.decompose_once(g(*qubits))) == set( gate.on_each(qubit_lists))
"op,expected", [ (cirq.H(Q), True), (cirq.HPowGate(exponent=0.5)(Q), False), (cirq.PhasedXPowGate(exponent=0.25, phase_exponent=0.125)(Q), True), (cirq.XPowGate(exponent=0.5)(Q), True), (cirq.YPowGate(exponent=0.25)(Q), True), (cirq.ZPowGate(exponent=0.125)(Q), True), (cirq.CZPowGate(exponent=0.5)(Q, Q2), False), (cirq.CZ(Q, Q2), True), (cirq.CNOT(Q, Q2), True), (cirq.SWAP(Q, Q2), False), (cirq.ISWAP(Q, Q2), False), (cirq.CCNOT(Q, Q2, Q3), True), (cirq.CCZ(Q, Q2, Q3), True), (cirq.ParallelGate(cirq.X, num_copies=3)(Q, Q2, Q3), True), (cirq.ParallelGate(cirq.Y, num_copies=3)(Q, Q2, Q3), True), (cirq.ParallelGate(cirq.Z, num_copies=3)(Q, Q2, Q3), True), (cirq.X(Q).controlled_by(Q2, Q3), True), (cirq.Z(Q).controlled_by(Q2, Q3), True), (cirq.ZPowGate(exponent=0.5)(Q).controlled_by(Q2, Q3), False), ], ) def test_gateset(op: cirq.Operation, expected: bool): gs = cirq_pasqal.PasqalGateset() assert gs.validate(op) == expected assert gs.validate(cirq.Circuit(op)) == expected @pytest.mark.parametrize( "op,expected",
def test_parallel_gate_op(gate, num_copies): qubits = cirq.LineQubit.range(num_copies * gate.num_qubits()) assert cirq.parallel_gate_op(gate, *qubits) == cirq.ParallelGate( gate, num_copies).on(*qubits)
import pytest import cirq import cirq.neutral_atoms.neutral_atom_gateset as nag Q = cirq.LineQubit.range(10) @pytest.mark.parametrize( 'op,is_in_gateset', ( (cirq.X.on(Q[0]), True), (cirq.Y.on(Q[0]), True), (cirq.I.on(Q[0]), True), (cirq.measure(*Q), True), (cirq.ParallelGate(cirq.X, 3).on(*Q[:3]), True), (cirq.ParallelGate(cirq.X, 4).on(*Q[:4]), False), (cirq.ParallelGate(cirq.X, 10).on(*Q), False), (cirq.ParallelGate(cirq.Y, 3).on(*Q[:3]), True), (cirq.ParallelGate(cirq.Y, 4).on(*Q[:4]), False), (cirq.ParallelGate(cirq.Y, 10).on(*Q), False), (cirq.ParallelGate(cirq.Z, 3).on(*Q[:3]), True), (cirq.ParallelGate(cirq.Z, 4).on(*Q[:4]), True), (cirq.ParallelGate(cirq.Z, 5).on(*Q[:5]), False), (cirq.ParallelGate(cirq.Z, 10).on(*Q), False), ( cirq.ParallelGate( cirq.PhasedXPowGate(exponent=0.5, phase_exponent=0.25), 3).on(*Q[:3]), True, ),
def test_parallel_gate_operation_is_consistent(gate, num_copies): cirq.testing.assert_implements_consistent_protocols( cirq.ParallelGate(gate, num_copies))
def test_str(): assert str(cirq.ParallelGate(cirq.X**0.5, 10)) == 'X**0.5 x 10'
def test_repr(): assert repr(cirq.ParallelGate( cirq.X, 2)) == 'cirq.ParallelGate(sub_gate=cirq.X, num_copies=2)'
def test_not_implemented_diagram(): q = cirq.LineQubit.range(2) g = cirq.testing.SingleQubitGate() c = cirq.Circuit() c.append(cirq.ParallelGate(g, 2)(*q)) assert 'cirq.testing.gate_features.SingleQubitGate ' in str(c)
cirq.AnyIntegerPowerGateFamily(gate=CustomXPowGate()) eq = cirq.testing.EqualsTester() gate_family = cirq.AnyIntegerPowerGateFamily(CustomXPowGate) eq.add_equality_group(gate_family) eq.add_equality_group(cirq.AnyIntegerPowerGateFamily(cirq.EigenGate)) cirq.testing.assert_equivalent_repr(gate_family) assert CustomX in gate_family assert CustomX**2 in gate_family assert CustomX**1.5 not in gate_family assert CustomX**sympy.Symbol('theta') not in gate_family assert 'CustomXPowGate' in gate_family.name assert '`g.exponent` is an integer' in gate_family.description @pytest.mark.parametrize( 'gate', [CustomX, cirq.ParallelGate(CustomX, 2), CustomXPowGate]) @pytest.mark.parametrize('name,description', [(None, None), ("Custom Name", "Custom Description")]) @pytest.mark.parametrize('max_parallel_allowed', [None, 3]) def test_parallel_gate_family(gate, name, description, max_parallel_allowed): gate_family = cirq.ParallelGateFamily( gate, name=name, description=description, max_parallel_allowed=max_parallel_allowed) cirq.testing.assert_equivalent_repr(gate_family) for gate_to_test in [CustomX, cirq.ParallelGate(CustomX, 2)]: assert gate_to_test in gate_family assert gate_to_test(*cirq.LineQubit.range(cirq.num_qubits( gate_to_test))) in gate_family
def test_parallel_gate_operation_init(gate, num_copies, qubits): v = cirq.ParallelGate(gate, num_copies) assert v.sub_gate == gate assert v.num_copies == num_copies assert v.on(*qubits).qubits == tuple(qubits)
def test_any_integer_power_gate_family(): with pytest.raises(ValueError, match='subclass of `cirq.EigenGate`'): cirq.AnyIntegerPowerGateFamily(gate=cirq.SingleQubitGate) with pytest.raises(ValueError, match='subclass of `cirq.EigenGate`'): cirq.AnyIntegerPowerGateFamily(gate=CustomXPowGate()) gate_family = cirq.AnyIntegerPowerGateFamily(CustomXPowGate) cirq.testing.assert_equivalent_repr(gate_family) assert CustomX in gate_family assert CustomX ** 2 in gate_family assert CustomX ** 1.5 not in gate_family assert CustomX ** sympy.Symbol('theta') not in gate_family assert 'CustomXPowGate' in gate_family.name assert '`g.exponent` is an integer' in gate_family.description @pytest.mark.parametrize('gate', [CustomX, cirq.ParallelGate(CustomX, 2), CustomXPowGate]) @pytest.mark.parametrize('name,description', [(None, None), ("Custom Name", "Custom Description")]) @pytest.mark.parametrize('max_parallel_allowed', [None, 3]) def test_parallel_gate_family(gate, name, description, max_parallel_allowed): gate_family = cirq.ParallelGateFamily( gate, name=name, description=description, max_parallel_allowed=max_parallel_allowed ) cirq.testing.assert_equivalent_repr(gate_family) for gate_to_test in [CustomX, cirq.ParallelGate(CustomX, 2)]: assert gate_to_test in gate_family assert gate_to_test(*cirq.LineQubit.range(cirq.num_qubits(gate_to_test))) in gate_family if isinstance(gate, cirq.ParallelGate) and not max_parallel_allowed: assert gate_family._max_parallel_allowed == cirq.num_qubits(gate) assert cirq.ParallelGate(CustomX, 4) not in gate_family else:
def test_invalid_parallel_gate_operation(gate, num_copies, qubits, error_msg): with pytest.raises(ValueError, match=error_msg): cirq.ParallelGate(gate, num_copies)(*qubits)
# limitations under the License. import numpy as np import pytest import cirq Q = cirq.LineQubit.range(3) @pytest.mark.parametrize( 'expected', ( cirq.Circuit(cirq.X.on(Q[0])), cirq.Circuit(cirq.Y.on(Q[0])), cirq.Circuit(cirq.ParallelGate(cirq.X, 3).on(*Q)), cirq.Circuit(cirq.CNOT.on(Q[0], Q[1])), ), ) def test_gates_preserved(expected: cirq.Circuit): actual = cirq.optimize_for_target_gateset( expected, gateset=cirq.neutral_atoms.NeutralAtomGateset()) assert actual == expected def test_coverage(): with cirq.testing.assert_deprecated("Use cirq.optimize_for_target_gateset", deadline='v0.16', count=5): q = cirq.LineQubit.range(3) g = cirq.testing.ThreeQubitGate()
def test_no_unitary(gate): g = cirq.ParallelGate(gate, 2) assert not cirq.has_unitary(g) assert cirq.unitary(g, None) is None