def test_validate_schedule_errors(): d = square_device(2, 2, max_controls=3) s = cirq.Schedule(device=cirq.UnconstrainedDevice) q00 = cirq.GridQubit(0, 0) q01 = cirq.GridQubit(0, 1) q10 = cirq.GridQubit(1, 0) q11 = cirq.GridQubit(1, 1) us = cirq.Duration(nanos=10**3) ms = cirq.Duration(nanos=10**6) msone = cirq.Timestamp(nanos=10**6) mstwo = cirq.Timestamp(nanos=2*10**6) msthree = cirq.Timestamp(nanos=3*10**6) for qubit in d.qubits: s.include(cirq.ScheduledOperation(cirq.Timestamp(nanos=0), 100*us, cirq.X.on(qubit))) s.include(cirq.ScheduledOperation(msone, 100*us, cirq.TOFFOLI.on(q00,q01,q10))) s.include(cirq.ScheduledOperation(mstwo, 100*us, cirq.ParallelGateOperation( cirq.X, [q00, q01]))) s.include(cirq.ScheduledOperation(mstwo, 100*us, cirq.ParallelGateOperation( cirq.Z, [q10, q11]))) for qubit in d.qubits: s.include(cirq.ScheduledOperation(msthree, 50*ms, cirq.GateOperation( cirq.MeasurementGate(1, qubit), [qubit]))) d.validate_schedule(s) s.include(cirq.ScheduledOperation(cirq.Timestamp(nanos=10**9), 100*us, cirq.X.on(q00))) with pytest.raises(ValueError, match="Non-measurement operation after " "measurement"): d.validate_schedule(s)
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.ParallelGateOperation( 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="Too many Z gates in parallel"): d.validate_operation(cirq.ParallelGateOperation(cirq.Z, d.qubits)) with pytest.raises(ValueError, match="Bad number of XY gates in parallel"): d.validate_operation( cirq.ParallelGateOperation(cirq.X, d.qubit_list()[1:]))
def test_with_qubits_and_transform_qubits(): g = cirq.SingleQubitGate() op = cirq.ParallelGateOperation(g, cirq.LineQubit.range(3)) line = cirq.LineQubit.range(3, 0, -1) negline = cirq.LineQubit.range(0, -3, -1) assert op.with_qubits(*line) == cirq.ParallelGateOperation(g, line) assert op.transform_qubits(lambda e: cirq.LineQubit(-e.x) ) == cirq.ParallelGateOperation(g, negline)
def test_extrapolate(): q = cirq.NamedQubit('q') # If the gate isn't extrapolatable, you get a type error. op0 = cirq.ParallelGateOperation(cirq.SingleQubitGate(), [q]) with pytest.raises(TypeError): _ = op0 ** 0.5 op = cirq.ParallelGateOperation(cirq.Y, [q]) assert op ** 0.5 == cirq.ParallelGateOperation(cirq.Y ** 0.5, [q]) assert cirq.inverse(op) == op ** -1 == cirq.ParallelGateOperation(cirq.Y ** -1, [q])
def test_with_qubits_and_transform_qubits(): with cirq.testing.assert_deprecated(deadline="v0.14", count=None): g = cirq.SingleQubitGate() op = cirq.ParallelGateOperation(g, cirq.LineQubit.range(3)) line = cirq.LineQubit.range(3, 0, -1) negline = cirq.LineQubit.range(0, -3, -1) assert op.with_qubits(*line) == cirq.ParallelGateOperation(g, line) assert op.transform_qubits( lambda e: cirq.LineQubit(-e.x)) == cirq.ParallelGateOperation( g, negline)
def test_invalid_parallel_gate_operation(): three_qubit_gate = cirq.ThreeQubitGate() single_qubit_gate = cirq.SingleQubitGate() repeated_qubits = [cirq.GridQubit(0, 0), cirq.GridQubit(0, 0)] with pytest.raises(ValueError) as wrong_gate: cirq.ParallelGateOperation(three_qubit_gate, cirq.NamedQubit("a")) assert str(wrong_gate.value) == "gate must be a single qubit gate" with pytest.raises(ValueError) as bad_qubits: cirq.ParallelGateOperation(single_qubit_gate, repeated_qubits) assert str(bad_qubits.value) == "repeated qubits are not allowed"
def test_phase(): g1 = cirq.SingleQubitGate() g2 = cirq.S g3 = cirq.phase_by(g2, 1, 0) qreg = cirq.LineQubit.range(2) op1 = cirq.ParallelGateOperation(g1, qreg) op2 = cirq.ParallelGateOperation(g2, qreg) assert cirq.phase_by(op2, 1, 0) == cirq.ParallelGateOperation(g3, qreg) with pytest.raises(TypeError): cirq.phase_by(op1, 1, 0)
def test_trace_distance(): s = cirq.X ** 0.25 twoop = cirq.ParallelGateOperation(s, cirq.LineQubit.range(2)) threeop = cirq.ParallelGateOperation(s, cirq.LineQubit.range(3)) fourop = cirq.ParallelGateOperation(s, cirq.LineQubit.range(4)) assert cirq.approx_eq(cirq.trace_distance_bound(twoop), np.sin(np.pi / 4)) assert cirq.approx_eq(cirq.trace_distance_bound(threeop), np.sin(3 * np.pi / 8)) assert cirq.approx_eq(cirq.trace_distance_bound(fourop), 1.0) foo = sympy.Symbol('foo') spo = cirq.ParallelGateOperation(cirq.X ** foo, cirq.LineQubit.range(4)) assert cirq.approx_eq(cirq.trace_distance_bound(spo), 1.0)
def test_gate_operation_eq(): g1 = cirq.SingleQubitGate() g2 = cirq.SingleQubitGate() r1 = [cirq.NamedQubit('r1')] r2 = [cirq.NamedQubit('r2')] r12 = r1 + r2 r21 = r2 + r1 eq = cirq.testing.EqualsTester() eq.make_equality_group(lambda: cirq.ParallelGateOperation(g1, r1)) eq.make_equality_group(lambda: cirq.ParallelGateOperation(g2, r1)) eq.add_equality_group(cirq.ParallelGateOperation(g1, r12), cirq.ParallelGateOperation(g1, r21))
def test_extrapolate(): with cirq.testing.assert_deprecated(deadline="v0.14", count=None): q = cirq.NamedQubit('q') # If the gate isn't extrapolatable, you get a type error. op0 = cirq.ParallelGateOperation(cirq.SingleQubitGate(), [q]) with pytest.raises(TypeError): _ = op0**0.5 op = cirq.ParallelGateOperation(cirq.Y, [q]) assert op**0.5 == cirq.ParallelGateOperation(cirq.Y**0.5, [q]) assert cirq.inverse(op) == op**-1 == cirq.ParallelGateOperation( cirq.Y**-1, [q])
def test_gate_operation_eq(): with cirq.testing.assert_deprecated(deadline="v0.14", count=None): g1 = cirq.SingleQubitGate() g2 = cirq.SingleQubitGate() r1 = [cirq.NamedQubit('r1')] r2 = [cirq.NamedQubit('r2')] r12 = r1 + r2 r21 = r2 + r1 eq = cirq.testing.EqualsTester() eq.make_equality_group(lambda: cirq.ParallelGateOperation(g1, r1)) eq.make_equality_group(lambda: cirq.ParallelGateOperation(g2, r1)) eq.add_equality_group(cirq.ParallelGateOperation(g1, r12), cirq.ParallelGateOperation(g1, r21))
def test_trace_distance(): with cirq.testing.assert_deprecated(deadline="v0.14", count=None): s = cirq.X**0.25 twoop = cirq.ParallelGateOperation(s, cirq.LineQubit.range(2)) threeop = cirq.ParallelGateOperation(s, cirq.LineQubit.range(3)) fourop = cirq.ParallelGateOperation(s, cirq.LineQubit.range(4)) assert cirq.approx_eq(cirq.trace_distance_bound(twoop), np.sin(np.pi / 4)) assert cirq.approx_eq(cirq.trace_distance_bound(threeop), np.sin(3 * np.pi / 8)) assert cirq.approx_eq(cirq.trace_distance_bound(fourop), 1.0) foo = sympy.Symbol('foo') spo = cirq.ParallelGateOperation(cirq.X**foo, cirq.LineQubit.range(4)) assert cirq.approx_eq(cirq.trace_distance_bound(spo), 1.0)
def test_parallel_gate_operation_init(): q = cirq.NamedQubit('q') r = cirq.NamedQubit('r') g = cirq.SingleQubitGate() v = cirq.ParallelGateOperation(g, (q, r)) assert v.gate == g assert v.qubits == (q, r)
def test_unitary(): a = cirq.NamedQubit('a') b = cirq.NamedQubit('b') g = cirq.SingleQubitGate() p = cirq.ParallelGateOperation(g, [a, b]) q = cirq.ParallelGateOperation(cirq.X, [a, b]) assert not cirq.has_unitary(p) assert cirq.unitary(p, None) is None np.testing.assert_allclose(cirq.unitary(q), np.array( [[0. + 0.j, 0. + 0.j, 0. + 0.j, 0. + 0.j], [0. + 0.j, 1. + 0.j, 1. + 0.j, 0. + 0.j], [0. + 0.j, 1. + 0.j, 1. + 0.j, 0. + 0.j], [0. + 0.j, 0. + 0.j, 0. + 0.j, 0. + 0.j]]), atol=1e-8)
def test_repr(): a, b = cirq.LineQubit.range(2) assert ( repr(cirq.ParallelGateOperation(cirq.X, (a, b))) == 'cirq.ParallelGateOperation(gate=cirq.X,' ' qubits=[cirq.LineQubit(0), cirq.LineQubit(1)])' )
def test_not_implemented_diagram(): with cirq.testing.assert_deprecated(deadline="v0.14", count=None): a = cirq.NamedQubit('a') g = cirq.SingleQubitGate() c = cirq.Circuit() c.append(cirq.ParallelGateOperation(g, [a])) assert 'cirq.ops.gate_features.SingleQubitGate' in str(c)
def test_parameterizable_effect(): q = cirq.NamedQubit('q') r = cirq.ParamResolver({'a': 0.5}) op1 = cirq.ParallelGateOperation(cirq.Z**sympy.Symbol('a'), [q]) assert cirq.is_parameterized(op1) op2 = cirq.resolve_parameters(op1, r) assert not cirq.is_parameterized(op2)
def test_parallel_gate_operation_init(): with cirq.testing.assert_deprecated(deadline="v0.14", count=None): q = cirq.NamedQubit('q') r = cirq.NamedQubit('r') g = cirq.SingleQubitGate() v = cirq.ParallelGateOperation(g, (q, r)) assert v.gate == g assert v.qubits == (q, r)
def test_validate_circuit_errors(): d = square_device(2, 2, max_controls=3) q00 = cirq.GridQubit(0, 0) q01 = cirq.GridQubit(0, 1) q10 = cirq.GridQubit(1, 0) q11 = cirq.GridQubit(1, 1) c = cirq.Circuit() c.append(cirq.ParallelGateOperation(cirq.X, d.qubits)) c.append(cirq.CCZ.on(q00, q01, q10)) c.append(cirq.ParallelGateOperation(cirq.Z, [q00, q01, q10])) m = cirq.Moment(cirq.X.on_each(q00, q01) + cirq.Z.on_each(q10, q11)) c.append(m) c.append(cirq.measure_each(*d.qubits)) d.validate_circuit(c) c.append(cirq.Moment([cirq.X.on(q00)])) with pytest.raises(ValueError, match="Non-empty moment after measurement"): d.validate_circuit(c)
def test_parameterizable_effect(resolve_fn): with cirq.testing.assert_deprecated(deadline="v0.14", count=None): q = cirq.NamedQubit('q') r = cirq.ParamResolver({'a': 0.5}) op1 = cirq.ParallelGateOperation(cirq.Z**sympy.Symbol('a'), [q]) assert cirq.is_parameterized(op1) op2 = resolve_fn(op1, r) assert not cirq.is_parameterized(op2)
def test_unitary(): with cirq.testing.assert_deprecated(deadline="v0.14", count=None): a = cirq.NamedQubit('a') b = cirq.NamedQubit('b') g = cirq.SingleQubitGate() p = cirq.ParallelGateOperation(g, [a, b]) q = cirq.ParallelGateOperation(cirq.X, [a, b]) assert not cirq.has_unitary(p) assert cirq.unitary(p, None) is None np.testing.assert_allclose( cirq.unitary(q), np.array([ [0.0 + 0.0j, 0.0 + 0.0j, 0.0 + 0.0j, 1.0 + 0.0j], [0.0 + 0.0j, 0.0 + 0.0j, 1.0 + 0.0j, 0.0 + 0.0j], [0.0 + 0.0j, 1.0 + 0.0j, 0.0 + 0.0j, 0.0 + 0.0j], [1.0 + 0.0j, 0.0 + 0.0j, 0.0 + 0.0j, 0.0 + 0.0j], ]), atol=1e-8, )
def test_validate_operation_errors(): d = cubic_device(3, 3, 3) qlist = d.qubit_list() too_many_qubits_op = cirq.ops.X.controlled(len(qlist) - 1) too_many_qubits_op = cirq.ops.GateOperation(too_many_qubits_op, qlist) with pytest.raises(ValueError, match="Too many qubits acted on in parallel by"): d.validate_operation(too_many_qubits_op) with pytest.raises(ValueError, match="Unsupported operation"): d.validate_operation(ThreeDGridQubit(0, 0, 0)) with pytest.raises(ValueError, match="cirq.H is not a supported gate"): d.validate_operation(cirq.ops.H.on(ThreeDGridQubit(0, 0, 0))) with pytest.raises(ValueError, match="is not a 3D grid qubit for gate cirq.X"): d.validate_operation(cirq.X.on(cirq.LineQubit(0))) with pytest.raises(ValueError, match="are too far away"): d.validate_operation( cirq.CZ.on(ThreeDGridQubit(0, 0, 0), ThreeDGridQubit(3, 3, 3))) with pytest.raises(ValueError, match="Too many Z gates in parallel"): d.validate_operation(cirq.ParallelGateOperation(cirq.ops.Z, d.qubits)) with pytest.raises(ValueError, match="Bad number of X/Y gates in parallel"): d.validate_operation( cirq.ParallelGateOperation(cirq.ops.X, d.qubit_list()[1:])) assert d.validate_operation( cirq.ops.GateOperation(cirq.ops.MeasurementGate(1), [ThreeDGridQubit(0, 0, 0)])) is None
def cnot_to_controlled_parallel_x(self, circuit, index, operation): p_op = cirq.ParallelGateOperation(cirq.ops.X, [operation.qubits[1]]) current_controlled_op = \ cirq.ControlledOperation([operation.qubits[0]], p_op) # print("A--------> ", len(circuit)) circuit.clear_operations_touching(operation.qubits, [index]) # print("B--------> ", len(circuit), repr(circuit)) # Is this an issue about how operations are inserted? circuit.insert(index + 1, current_controlled_op, strategy=cirq.InsertStrategy.INLINE) # print("C--------> ", len(circuit), repr(circuit)) return current_controlled_op
def merge_controlled_parallel_x(self, op1, op2): if (len(op1.controls) != len(op2.controls)) \ and (len(op1.controls) != 1) \ and op1.controls[0] != op2.controls[0]: return merged_qubits = op1.sub_operation.qubits + \ op2.sub_operation.qubits try: p_op = cirq.ParallelGateOperation(cirq.ops.X, merged_qubits) except ValueError as e: print(e, merged_qubits) merged_op = cirq.ControlledOperation([op1.controls[0]], sub_operation=p_op) return merged_op
def test_serialize_non_gate_op_invalid(): q0, q1 = cirq.LineQubit.range(2) circuit = cirq.Circuit(cirq.ParallelGateOperation(cirq.X, [q0, q1])) serializer = ionq.Serializer() with pytest.raises(ValueError, match='ParallelGateOperation'): _ = serializer.serialize(circuit)
def test_decompose(): qreg = cirq.LineQubit.range(3) op = cirq.ParallelGateOperation(cirq.X, qreg) assert cirq.decompose(op) == cirq.X.on_each(*qreg)
def test_not_implemented_diagram(): a = cirq.NamedQubit('a') g = cirq.SingleQubitGate() c = cirq.Circuit() c.append(cirq.ParallelGateOperation(g, [a])) assert 'cirq.ops.gate_features.SingleQubitGate' in str(c)
def test_str(): a, b = cirq.LineQubit.range(2) assert str(cirq.ParallelGateOperation(cirq.X, (a, b))) == 'X(0, 1)'
prefix = gates[:i + 1] expected_a = cirq.LinearCombinationOfGates(collections.Counter(prefix)) expected_b = -expected_a assert_linear_combinations_are_equal(a, expected_a) assert_linear_combinations_are_equal(b, expected_b) @pytest.mark.parametrize('op', ( cirq.X(q0), cirq.Y(q1), cirq.XX(q0, q1), cirq.CZ(q0, q1), cirq.FREDKIN(q0, q1, q2), cirq.ControlledOperation((q0, q1), cirq.H(q2)), cirq.ParallelGateOperation(cirq.X, (q0, q1, q2)), cirq.PauliString({ q0: cirq.X, q1: cirq.Y, q2: cirq.Z }), )) def test_empty_linear_combination_of_operations_accepts_all_operations(op): combination = cirq.LinearCombinationOfOperations({}) combination[op] = -0.5j assert len(combination) == 1 @pytest.mark.parametrize('terms', ( { cirq.X(q0): -2,
def test_parallel_gate_operation_is_consistent(gate, qubits): op = cirq.ParallelGateOperation(gate, qubits) cirq.testing.assert_implements_consistent_protocols(op)