def test_query_timedelta(): q = cirq.NamedQubit('q') zero = cirq.Timestamp(picos=0) ms = timedelta(microseconds=1000) op1 = cirq.ScheduledOperation(zero, 2 * ms, cirq.H(q)) op2 = cirq.ScheduledOperation(zero + ms, 2 * ms, cirq.H(q)) schedule = cirq.Schedule(device=UNCONSTRAINED_DEVICE, scheduled_operations=[op2, op1]) def query(t, d=timedelta(), qubits=None): return schedule.query(time=t, duration=d, qubits=qubits, include_query_end_time=True, include_op_end_times=True) assert query(zero - 0.5 * ms, ms) == [op1] assert query(zero - 0.5 * ms, 2 * ms) == [op1, op2] assert query(zero, ms) == [op1, op2] assert query(zero + 0.5 * ms, ms) == [op1, op2] assert query(zero + ms, ms) == [op1, op2] assert query(zero + 1.5 * ms, ms) == [op1, op2] assert query(zero + 2.0 * ms, ms) == [op1, op2] assert query(zero + 2.5 * ms, ms) == [op2] assert query(zero + 3.0 * ms, ms) == [op2] assert query(zero + 3.5 * ms, ms) == []
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_query_point_operation_exclusive(): q = cirq.NamedQubit('q') zero = cirq.Timestamp(picos=0) ps = cirq.Duration(picos=1) op = cirq.ScheduledOperation(zero, cirq.Duration(), cirq.H(q)) schedule = cirq.Schedule(device=UNCONSTRAINED_DEVICE, scheduled_operations=[op]) assert schedule.query(time=zero, include_query_end_time=False, include_op_end_times=False) == [] assert schedule.query(time=zero + ps, include_query_end_time=False, include_op_end_times=False) == [] assert schedule.query(time=zero - ps, include_query_end_time=False, include_op_end_times=False) == [] assert schedule.query(time=zero) == [] assert schedule.query(time=zero + ps) == [] assert schedule.query(time=zero - ps) == [] assert schedule.query(time=zero, qubits=[]) == [] assert schedule.query(time=zero, qubits=[q]) == [] assert schedule.query(time=zero, duration=ps) == [] assert schedule.query(time=zero - 0.5 * ps, duration=ps) == [op] assert schedule.query(time=zero - ps, duration=ps) == [] assert schedule.query(time=zero - 2 * ps, duration=ps) == [] assert schedule.query(time=zero - ps, duration=3 * ps) == [op] assert schedule.query(time=zero + ps, duration=ps) == []
def test_query_point_operation_inclusive(): q = cirq.NamedQubit('q') zero = cirq.Timestamp(picos=0) ps = cirq.Duration(picos=1) op = cirq.ScheduledOperation(zero, cirq.Duration(), cirq.H(q)) schedule = cirq.Schedule(device=UNCONSTRAINED_DEVICE, scheduled_operations=[op]) def query(t, d=cirq.Duration(), qubits=None): return schedule.query(time=t, duration=d, qubits=qubits, include_query_end_time=True, include_op_end_times=True) assert query(zero) == [op] assert query(zero + ps) == [] assert query(zero - ps) == [] assert query(zero, qubits=[]) == [] assert query(zero, qubits=[q]) == [op] assert query(zero, ps) == [op] assert query(zero - 0.5 * ps, ps) == [op] assert query(zero - ps, ps) == [op] assert query(zero - 2 * ps, ps) == [] assert query(zero - ps, 3 * ps) == [op] assert query(zero + ps, ps) == []
def test_query_overlapping_operations_inclusive(): q = cirq.NamedQubit('q') zero = cirq.Timestamp(picos=0) ps = cirq.Duration(picos=1) op1 = cirq.ScheduledOperation(zero, 2 * ps, cirq.H(q)) op2 = cirq.ScheduledOperation(zero + ps, 2 * ps, cirq.H(q)) schedule = cirq.Schedule(device=UNCONSTRAINED_DEVICE, scheduled_operations=[op2, op1]) def query(t, d=cirq.Duration(), qubits=None): return schedule.query(time=t, duration=d, qubits=qubits, include_query_end_time=True, include_op_end_times=True) assert query(zero - 0.5 * ps, ps) == [op1] assert query(zero - 0.5 * ps, 2 * ps) == [op1, op2] assert query(zero, ps) == [op1, op2] assert query(zero + 0.5 * ps, ps) == [op1, op2] assert query(zero + ps, ps) == [op1, op2] assert query(zero + 1.5 * ps, ps) == [op1, op2] assert query(zero + 2.0 * ps, ps) == [op1, op2] assert query(zero + 2.5 * ps, ps) == [op2] assert query(zero + 3.0 * ps, ps) == [op2] assert query(zero + 3.5 * ps, ps) == []
def test_validate_scheduled_operation_errors(): d = square_device(2, 2) s = cirq.Schedule(device=cirq.UnconstrainedDevice) q00 = cirq.GridQubit(0, 0) so = cirq.ScheduledOperation(cirq.Timestamp(), cirq.Duration(nanos=1), cirq.X.on(q00)) with pytest.raises(ValueError, match="Incompatible operation duration"): d.validate_scheduled_operation(s, so)
def test_schedule_and_device_both_not_supported(build): scheduled_op = cirq.ScheduledOperation(time=None, duration=None, operation=cirq.H.on( cirq.NamedQubit("dorothy"))) schedule = cirq.Schedule(device=Foxtail, scheduled_operations=[scheduled_op]) eng = Engine(api_key="key") with pytest.raises(ValueError, match='Device'): eng.run(schedule, JobConfig('project-id'), device=Foxtail)
def test_validate_scheduled_operation_adjacent_exp_11_exp_z(): d = square_device(3, 3, holes=[cirq.GridQubit(1, 1)]) q0 = cirq.GridQubit(0, 0) q1 = cirq.GridQubit(1, 0) q2 = cirq.GridQubit(2, 0) s = cirq.Schedule(d, [ cirq.ScheduledOperation.op_at_on(cirq.Z(q0), cirq.Timestamp(), d), cirq.ScheduledOperation.op_at_on(cirq.CZ(q1, q2), cirq.Timestamp(), d), ]) d.validate_schedule(s)
def test_schedule_device_validation_fails(build): scheduled_op = cirq.ScheduledOperation(time=None, duration=None, operation=cirq.H.on( cirq.NamedQubit("dorothy"))) schedule = cirq.Schedule(device=Foxtail, scheduled_operations=[scheduled_op]) with pytest.raises(ValueError): Engine(api_key="key").run(schedule, JobConfig('project-id'))
def test_schedule_device_validation_fails(build): scheduled_op = cirq.ScheduledOperation(time=None, duration=cirq.Duration(), operation=cirq.H.on( cirq.NamedQubit("dorothy"))) schedule = cirq.Schedule(device=cg.Foxtail, scheduled_operations=[scheduled_op]) with pytest.raises(ValueError): cg.Engine(project_id='project-id').run(program=schedule)
def test_unitary(): q = cirq.NamedQubit('q') zero = cirq.Timestamp(picos=0) ps = cirq.Duration(picos=1) op = cirq.ScheduledOperation(zero, ps, cirq.H(q)) schedule = cirq.Schedule(device=UNCONSTRAINED_DEVICE, scheduled_operations=[op]) cirq.testing.assert_has_consistent_apply_unitary(schedule, qubit_count=1) np.testing.assert_allclose(cirq.unitary(schedule), cirq.unitary(cirq.H)) assert cirq.has_unitary(schedule) schedule2 = cirq.Schedule(device=UNCONSTRAINED_DEVICE, scheduled_operations=[ cirq.ScheduledOperation( zero, ps, cirq.depolarize(0.5).on(q)) ]) assert not cirq.has_unitary(schedule2)
def test_validate_scheduled_operation_not_adjacent_exp_11_exp_w(): d = square_device(3, 3, holes=[cirq.GridQubit(1, 1)]) q0 = cirq.GridQubit(0, 0) p1 = cirq.GridQubit(1, 2) p2 = cirq.GridQubit(2, 2) s = cirq.Schedule(d, [ cirq.ScheduledOperation.op_at_on(cirq.X(q0), cirq.Timestamp(), d), cirq.ScheduledOperation.op_at_on(cirq.CZ(p1, p2), cirq.Timestamp(), d), ]) d.validate_schedule(s)
def test_the_test_device(): device = _TestDevice() device.validate_gate(cirq.H) with pytest.raises(ValueError): device.validate_gate(cirq.X) device.validate_operation(cirq.H(cirq.LineQubit(0))) with pytest.raises(ValueError): device.validate_operation(NotImplementedOperation()) device.validate_schedule(cirq.Schedule(device, [])) device.validate_schedule( cirq.Schedule(device, [ cirq.ScheduledOperation.op_at_on(cirq.H(cirq.LineQubit(0)), cirq.Timestamp(), device), cirq.ScheduledOperation.op_at_on( cirq.CZ(cirq.LineQubit(0), cirq.LineQubit(1)), cirq.Timestamp(), device) ])) with pytest.raises(ValueError): device.validate_schedule( cirq.Schedule(device, [ cirq.ScheduledOperation.op_at_on(NotImplementedOperation(), cirq.Timestamp(), device) ])) with pytest.raises(ValueError): device.validate_schedule( cirq.Schedule(device, [ cirq.ScheduledOperation.op_at_on(cirq.X(cirq.LineQubit(0)), cirq.Timestamp(), device) ])) with pytest.raises(ValueError): device.validate_schedule( cirq.Schedule(device, [ cirq.ScheduledOperation.op_at_on(cirq.H(cirq.NamedQubit('q')), cirq.Timestamp(), device) ])) with pytest.raises(ValueError): device.validate_schedule( cirq.Schedule(device, [ cirq.ScheduledOperation.op_at_on(cirq.H(cirq.LineQubit(100)), cirq.Timestamp(), device) ])) with pytest.raises(ValueError): device.validate_schedule( cirq.Schedule(device, [ cirq.ScheduledOperation.op_at_on( cirq.CZ(cirq.LineQubit(1), cirq.LineQubit(3)), cirq.Timestamp(), device) ]))
def simple_schedule(q, start_picos=0, duration_picos=1, num_ops=1): time_picos = start_picos scheduled_ops = [] for _ in range(num_ops): op = cirq.ScheduledOperation(cirq.Timestamp(picos=time_picos), cirq.Duration(picos=duration_picos), cirq.H(q)) scheduled_ops.append(op) time_picos += duration_picos return cirq.Schedule(device=UnconstrainedDevice, scheduled_operations=scheduled_ops)
def test_validate_scheduled_operation_XXPow_on_same_qubit(): d = ion_device(3) q0 = cirq.LineQubit(0) q1 = cirq.LineQubit(1) q2 = cirq.LineQubit(2) s = cirq.Schedule(d, [ cirq.ScheduledOperation.op_at_on(cirq.XX(q0, q1), cirq.Timestamp(), d), cirq.ScheduledOperation.op_at_on(cirq.XX(q1, q2), cirq.Timestamp(), d), ]) with pytest.raises(ValueError): d.validate_schedule(s)
def simple_schedule(q, start_picos=0, duration_micros=1, num_ops=1): time_picos = start_picos scheduled_ops = [] for _ in range(num_ops): op = cirq.ScheduledOperation( cirq.Timestamp(picos=time_picos), timedelta(microseconds=duration_micros), cirq.H(q)) scheduled_ops.append(op) time_picos += duration_micros * 10**6 return cirq.Schedule(device=UNCONSTRAINED_DEVICE, scheduled_operations=scheduled_ops)
def test_validate_schedule_repeat_measurement_keys(): d = ion_device(3) s = cirq.Schedule(d, [ cirq.ScheduledOperation.op_at_on( cirq.measure(cirq.LineQubit(0), key='a'), cirq.Timestamp(), d), cirq.ScheduledOperation.op_at_on( cirq.measure(cirq.LineQubit(1), key='a'), cirq.Timestamp(), d), ]) with pytest.raises(ValueError, match='Measurement key a repeated'): d.validate_schedule(s)
def test_validate_scheduled_operation_adjacent_exp_11s_not_allowed(): d = square_device(3, 3, holes=[cirq.GridQubit(1, 1)]) q0 = cirq.GridQubit(0, 0) q1 = cirq.GridQubit(0, 1) p0 = cirq.GridQubit(1, 0) p1 = cirq.GridQubit(1, 1) s = cirq.Schedule(d, [ cirq.ScheduledOperation.op_at_on(cirq.CZ(q0, q1), cirq.Timestamp(), d), cirq.ScheduledOperation.op_at_on(cirq.CZ(p0, p1), cirq.Timestamp(), d), ]) with pytest.raises(ValueError, match='Adjacent Exp11 operations'): d.validate_schedule(s)
def test_validate_scheduled_operation_adjacent_exp_11_measure(): d = square_device(3, 3, holes=[cirq.GridQubit(1, 1)]) q0 = cirq.GridQubit(0, 0) q1 = cirq.GridQubit(1, 0) q2 = cirq.GridQubit(2, 0) s = cirq.Schedule(d, [ cirq.ScheduledOperation.op_at_on(cg.XmonMeasurementGate().on(q0), cirq.Timestamp(), d), cirq.ScheduledOperation.op_at_on(cg.Exp11Gate().on(q1, q2), cirq.Timestamp(), d), ]) d.validate_schedule(s)
def test_validate_schedule_repeat_measurement_keys(): d = square_device(3, 3) s = cirq.Schedule(d, [ cirq.ScheduledOperation.op_at_on( cirq.measure(cirq.GridQubit(0, 0), key='a'), cirq.Timestamp(), d), cirq.ScheduledOperation.op_at_on( cirq.measure(cirq.GridQubit(0, 1), key='a'), cirq.Timestamp(), d), ]) with pytest.raises(ValueError, message='Measurement key a repeated'): d.validate_schedule(s)
def test_validate_scheduled_operation_adjacent_XXPow_Z(): d = ion_device(3) q0 = cirq.LineQubit(0) q1 = cirq.LineQubit(1) q2 = cirq.LineQubit(2) s = cirq.Schedule(d, [ cirq.ScheduledOperation.op_at_on(cirq.Z(q0), cirq.Timestamp(), d), cirq.ScheduledOperation.op_at_on(cirq.XX(q1, q2), cirq.Timestamp(), d), cirq.ScheduledOperation.op_at_on(cirq.X(q1), cirq.Timestamp(), d), cirq.ScheduledOperation.op_at_on(cirq.measure(q2), cirq.Timestamp(), d), ]) d.validate_schedule(s)
def test_serialize_deserialize_empty_schedule(): schedule = cirq.Schedule(device=cirq.google.Bristlecone, scheduled_operations=[]) proto = { 'language': { 'arg_function_language': '', 'gate_set': 'my_gate_set' } } assert proto == MY_GATE_SET.serialize_dict(schedule) with pytest.raises(ValueError): MY_GATE_SET.deserialize_dict(proto, cirq.google.Bristlecone)
def test_serialize_deserialize_empty_schedule(): schedule = cirq.Schedule(device=cirq.google.Bristlecone, scheduled_operations=[]) proto = { 'language': { 'gate_set': 'my_gate_set' }, 'schedule': { 'scheduled_operations': [] }, } assert proto == MY_GATE_SET.serialize(schedule) assert MY_GATE_SET.deserialize(proto, cirq.google.Bristlecone) == schedule
def test_serialize_schedule(): q0 = cirq.GridQubit(1, 1) q1 = cirq.GridQubit(1, 2) scheduled_ops = [ cirq.ScheduledOperation.op_at_on(cirq.CZ(q0, q1), cirq.Timestamp(nanos=0), device=cg.Bristlecone), cirq.ScheduledOperation.op_at_on(cirq.X(q0), cirq.Timestamp(nanos=200), device=cg.Bristlecone), cirq.ScheduledOperation.op_at_on(cirq.Z(q1), cirq.Timestamp(nanos=200), device=cg.Bristlecone), cirq.ScheduledOperation.op_at_on(cirq.measure(q0, key='a'), cirq.Timestamp(nanos=400), device=cg.Bristlecone) ] schedule = cirq.Schedule(device=cirq.google.Bristlecone, scheduled_operations=scheduled_ops) expected = { 'language': { 'arg_function_language': '', 'gate_set': 'xmon' }, 'schedule': { 'scheduled_operations': [{ 'operation': cg.XMON.serialize_op_dict(cirq.CZ(q0, q1)), 'start_time_picos': '0' }, { 'operation': cg.XMON.serialize_op_dict(cirq.X(q0)), 'start_time_picos': '200000', }, { 'operation': cg.XMON.serialize_op_dict(cirq.Z(q1)), 'start_time_picos': '200000', }, { 'operation': cg.XMON.serialize_op_dict(cirq.measure(q0, key='a')), 'start_time_picos': '400000', }] }, } assert cg.XMON.serialize_dict(schedule) == expected
def test_exclude(): q = cirq.QubitId() zero = cirq.Timestamp(picos=0) ps = cirq.Duration(picos=1) op = cirq.ScheduledOperation(zero, ps, cirq.H(q)) schedule = cirq.Schedule(device=UnconstrainedDevice, scheduled_operations=[op]) assert not schedule.exclude( cirq.ScheduledOperation(zero + ps, ps, cirq.H(q))) assert not schedule.exclude(cirq.ScheduledOperation(zero, ps, cirq.X(q))) assert schedule.query(time=zero, duration=ps * 10) == [op] assert schedule.exclude(cirq.ScheduledOperation(zero, ps, cirq.H(q))) assert schedule.query(time=zero, duration=ps * 10) == [] assert not schedule.exclude(cirq.ScheduledOperation(zero, ps, cirq.H(q)))
def test_exclude(): q = cirq.NamedQubit('q') zero = cirq.Timestamp(picos=0) ps = cirq.Duration(picos=1) op = cirq.ScheduledOperation(zero, ps, cirq.H(q)) schedule = cirq.Schedule(device=UNCONSTRAINED_DEVICE, scheduled_operations=[op]) assert not schedule.exclude( cirq.ScheduledOperation(zero + ps, ps, cirq.H(q))) assert not schedule.exclude(cirq.ScheduledOperation(zero, ps, cirq.X(q))) assert schedule.query(time=zero, duration=ps * 10) == [op] assert schedule.exclude(cirq.ScheduledOperation(zero, ps, cirq.H(q))) assert schedule.query(time=zero, duration=ps * 10) == [] assert not schedule.exclude(cirq.ScheduledOperation(zero, ps, cirq.H(q)))
def test_deserialize_schedule(): q0 = cirq.GridQubit(1, 1) q1 = cirq.GridQubit(1, 2) scheduled_ops = [ cirq.ScheduledOperation.op_at_on(cirq.CZ(q0, q1), cirq.Timestamp(nanos=0), device=cg.Bristlecone), cirq.ScheduledOperation.op_at_on(cirq.X(q0), cirq.Timestamp(nanos=200), device=cg.Bristlecone), cirq.ScheduledOperation.op_at_on(cirq.Z(q1), cirq.Timestamp(nanos=200), device=cg.Bristlecone), cirq.ScheduledOperation.op_at_on(cirq.measure(q0, key='a'), cirq.Timestamp(nanos=400), device=cg.Bristlecone) ] schedule = cirq.Schedule(device=cirq.google.Bristlecone, scheduled_operations=scheduled_ops) serialized = { 'language': { 'gate_set': 'xmon' }, 'schedule': { 'scheduled_operations': [{ 'operation': cg.XMON.serialize_op(cirq.CZ(q0, q1)), 'start_time_picos': 0 }, { 'operation': cg.XMON.serialize_op(cirq.X(q0)), 'start_time_picos': 200000, }, { 'operation': cg.XMON.serialize_op(cirq.Z(q1)), 'start_time_picos': 200000, }, { 'operation': cg.XMON.serialize_op(cirq.measure(q0, key='a')), 'start_time_picos': 400000, }] }, } assert cg.XMON.deserialize(serialized, cg.Bristlecone) == schedule
def test_find_measurements_simple_schedule(): schedule = cirq.Schedule( device=cirq.UNCONSTRAINED_DEVICE, scheduled_operations=[ cirq.ScheduledOperation( time=cirq.Timestamp(picos=10_000), duration=cirq.Duration(nanos=1000), operation=cirq.measure(q(0, 0), q(0, 1), q(0, 2), key='k'), ), ], ) measurements = v2.find_measurements(schedule) assert len(measurements) == 1 m = measurements[0] _check_measurement(m, 'k', [q(0, 0), q(0, 1), q(0, 2)], 10_000)
def test_query_overlapping_operations_exclusive(): q = cirq.QubitId() zero = cirq.Timestamp(picos=0) ps = cirq.Duration(picos=1) op1 = cirq.ScheduledOperation(zero, 2 * ps, cirq.H(q)) op2 = cirq.ScheduledOperation(zero + ps, 2 * ps, cirq.H(q)) schedule = cirq.Schedule(device=UnconstrainedDevice, scheduled_operations=[op2, op1]) assert schedule.query(time=zero - 0.5 * ps, duration=ps) == [op1] assert schedule.query(time=zero - 0.5 * ps, duration=2 * ps) == [op1, op2] assert schedule.query(time=zero, duration=ps) == [op1] assert schedule.query(time=zero + 0.5 * ps, duration=ps) == [op1, op2] assert schedule.query(time=zero + ps, duration=ps) == [op1, op2] assert schedule.query(time=zero + 1.5 * ps, duration=ps) == [op1, op2] assert schedule.query(time=zero + 2.0 * ps, duration=ps) == [op2] assert schedule.query(time=zero + 2.5 * ps, duration=ps) == [op2] assert schedule.query(time=zero + 3.0 * ps, duration=ps) == [] assert schedule.query(time=zero + 3.5 * ps, duration=ps) == []
def test_query_overlapping_operations_exclusive(): q = cirq.NamedQubit('q') zero = cirq.Timestamp(picos=0) ps = cirq.Duration(picos=1) op1 = cirq.ScheduledOperation(zero, 2 * ps, cirq.H(q)) op2 = cirq.ScheduledOperation(zero + ps, 2 * ps, cirq.H(q)) schedule = cirq.Schedule(device=UNCONSTRAINED_DEVICE, scheduled_operations=[op2, op1]) assert schedule.query(time=zero - 0.5 * ps, duration=ps) == [op1] assert schedule.query(time=zero - 0.5 * ps, duration=2 * ps) == [op1, op2] assert schedule.query(time=zero, duration=ps) == [op1] assert schedule.query(time=zero + 0.5 * ps, duration=ps) == [op1, op2] assert schedule.query(time=zero + ps, duration=ps) == [op1, op2] assert schedule.query(time=zero + 1.5 * ps, duration=ps) == [op1, op2] assert schedule.query(time=zero + 2.0 * ps, duration=ps) == [op2] assert schedule.query(time=zero + 2.5 * ps, duration=ps) == [op2] assert schedule.query(time=zero + 3.0 * ps, duration=ps) == [] assert schedule.query(time=zero + 3.5 * ps, duration=ps) == []