def test_init_timedelta(): r = ScheduledOperation(time=Timestamp(picos=5), duration=timedelta(microseconds=7), operation=cirq.H(cirq.NamedQubit('a'))) assert r.time == Timestamp(picos=5) assert r.duration == Duration(picos=7 * 10**6) assert r.operation == cirq.H(cirq.NamedQubit('a'))
def test_init(): r = ScheduledOperation(time=Timestamp(picos=5), duration=Duration(picos=7), operation=cirq.H(cirq.NamedQubit('a'))) assert r.time == Timestamp(picos=5) assert r.duration == Duration(picos=7) assert r.operation == cirq.H(cirq.NamedQubit('a'))
def test_init(): r = ScheduledOperation(time=Timestamp(picos=5), duration=Duration(picos=7), operation=ops.Operation(ops.H, [ops.NamedQubit('a')])) assert r.time == Timestamp(picos=5) assert r.duration == Duration(picos=7) assert r.operation == ops.Operation(ops.H, [ops.NamedQubit('a')])
def test_validate_scheduled_operation_not_adjacent_exp_11_exp_w(): d = square_device(3, 3, holes=[XmonQubit(1, 1)]) q0 = XmonQubit(0, 0) p1 = XmonQubit(1, 2) p2 = XmonQubit(2, 2) s = Schedule(d, [ ScheduledOperation.op_at_on(ExpWGate().on(q0), Timestamp(), d), ScheduledOperation.op_at_on(Exp11Gate().on(p1, p2), Timestamp(), d), ]) d.validate_schedule(s)
def test_validate_scheduled_operation_adjacent_exp_11_exp_z(): d = square_device(3, 3, holes=[XmonQubit(1, 1)]) q0 = XmonQubit(0, 0) q1 = XmonQubit(1, 0) q2 = XmonQubit(2, 0) s = Schedule(d, [ ScheduledOperation.op_at_on(ExpZGate().on(q0), Timestamp(), d), ScheduledOperation.op_at_on(Exp11Gate().on(q1, q2), Timestamp(), d), ]) d.validate_schedule(s)
def test_eq(): q0 = cirq.QubitId() eq = EqualsTester() eq.make_equality_group( lambda: ScheduledOperation(Timestamp(), Duration(), cirq.H(q0))) eq.make_equality_group( lambda: ScheduledOperation(Timestamp(picos=5), Duration(), cirq.H(q0))) eq.make_equality_group( lambda: ScheduledOperation(Timestamp(), Duration(picos=5), cirq.H(q0))) eq.make_equality_group( lambda: ScheduledOperation(Timestamp(), Duration(), cirq.X(q0)))
def test_validate_schedule_repeat_measurement_keys(): d = square_device(3, 3) s = Schedule(d, [ ScheduledOperation.op_at_on( XmonMeasurementGate('a').on(XmonQubit(0, 0)), Timestamp(), d), ScheduledOperation.op_at_on( XmonMeasurementGate('a').on(XmonQubit(0, 1)), Timestamp(), d), ]) with pytest.raises(ValueError, message='Measurement key a repeated'): d.validate_schedule(s)
def test_eq(): q0 = ops.QubitId() eq = EqualsTester() eq.make_equality_pair( lambda: ScheduledOperation(Timestamp(), Duration(), ops.H(q0))) eq.make_equality_pair( lambda: ScheduledOperation(Timestamp(picos=5), Duration(), ops.H(q0))) eq.make_equality_pair( lambda: ScheduledOperation(Timestamp(), Duration(picos=5), ops.H(q0))) eq.make_equality_pair( lambda: ScheduledOperation(Timestamp(), Duration(), ops.X(q0)))
def moment_by_moment_schedule(device: Device, circuit: Circuit): """Returns a schedule aligned with the moment structure of the Circuit. This method attempts to create a schedule in which each moment of a circuit is scheduled starting at the same time. Given the constraints of the given device, such a schedule may not be possible, in this case the the method will raise a ValueError with a description of the conflict. The schedule that is produced will take each moments and schedule the operations in this moment in a time slice of length equal to the maximum time of an operation in the moment. Returns: A Schedule for the circuit. Raises: ValueError: if the scheduling cannot be done. """ schedule = Schedule(device) t = Timestamp() for moment in circuit.moments: if not moment.operations: continue for op in moment.operations: scheduled_op = ScheduledOperation.op_at_on(op, t, device) # Raises a ValueError describing the problem if this cannot be # scheduled. schedule.include(scheduled_operation=scheduled_op) # Raises ValueError at first sign of a device conflict. device.validate_scheduled_operation(schedule, scheduled_op) # Increment time for next moment by max of ops during this moment. max_duration = max(device.duration_of(op) for op in moment.operations) t += max_duration return schedule
def test_query_overlapping_operations_inclusive(): q = ops.QubitId() zero = Timestamp(picos=0) ps = Duration(picos=1) op1 = ScheduledOperation(zero, 2 * ps, ops.H(q)) op2 = ScheduledOperation(zero + ps, 2 * ps, ops.H(q)) schedule = Schedule(device=UnconstrainedDevice, scheduled_operations=[op2, op1]) def query(t, d=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_query_point_operation_inclusive(): q = ops.QubitId() zero = Timestamp(picos=0) ps = Duration(picos=1) op = ScheduledOperation(zero, Duration(), ops.H(q)) schedule = Schedule(device=UnconstrainedDevice, scheduled_operations=[op]) def query(t, d=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_moment_by_moment_schedule_empty_moment_ignored(): device = _TestDevice() qubits = device.qubits circuit = Circuit( [Moment([ops.H(qubits[0])]), Moment([]), Moment([ops.H(qubits[0])])]) schedule = moment_by_moment_schedule(device, circuit) zero_ns = Timestamp() twenty_ns = Timestamp(nanos=20) assert set(schedule.scheduled_operations) == { ScheduledOperation.op_at_on(ops.H(qubits[0]), zero_ns, device), ScheduledOperation.op_at_on(ops.H(qubits[0]), twenty_ns, device), }
def test_query_point_operation_exclusive(): q = ops.QubitId() zero = Timestamp(picos=0) ps = Duration(picos=1) op = ScheduledOperation(zero, Duration(), ops.H(q)) schedule = Schedule(device=UnconstrainedDevice, 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 simple_schedule(q, start_picos=0, duration_picos=1, num_ops=1): time_picos = start_picos scheduled_ops = [] for _ in range(num_ops): op = ScheduledOperation(Timestamp(picos=time_picos), Duration(picos=duration_picos), ops.H(q)) scheduled_ops.append(op) time_picos += duration_picos return Schedule(device=UnconstrainedDevice, scheduled_operations=scheduled_ops)
def test_moment_by_moment_schedule_two_moments(): device = _TestDevice() qubits = device.qubits circuit = Circuit([ Moment(ops.H(q) for q in qubits), Moment((ops.CZ(qubits[i], qubits[i + 1]) for i in range(0, 9, 3))) ]) schedule = moment_by_moment_schedule(device, circuit) zero_ns = Timestamp() twenty_ns = Timestamp(nanos=20) expected_one_qubit = set( ScheduledOperation.op_at_on(ops.H(q), zero_ns, device) for q in qubits) expected_two_qubit = set( ScheduledOperation.op_at_on(ops.CZ(qubits[i], qubits[i + 1]), twenty_ns, device) for i in range(0, 9, 3)) expected = expected_one_qubit.union(expected_two_qubit) assert set(schedule.scheduled_operations) == expected
def test_moment_by_moment_schedule_max_duration(): device = _TestDevice() qubits = device.qubits circuit = Circuit([ Moment([ops.H(qubits[0]), ops.CZ(qubits[1], qubits[2])]), Moment([ops.H(qubits[0])]) ]) schedule = moment_by_moment_schedule(device, circuit) zero_ns = Timestamp() fourty_ns = Timestamp(nanos=40) assert set(schedule.scheduled_operations) == { ScheduledOperation.op_at_on(ops.H(qubits[0]), zero_ns, device), ScheduledOperation.op_at_on(ops.CZ(qubits[1], qubits[2]), zero_ns, device), ScheduledOperation.op_at_on(ops.H(qubits[0]), fourty_ns, device), }
def test_exclude(): q = ops.QubitId() zero = Timestamp(picos=0) ps = Duration(picos=1) op = ScheduledOperation(zero, ps, ops.H(q)) schedule = Schedule(device=UnconstrainedDevice, scheduled_operations=[op]) assert not schedule.exclude(ScheduledOperation(zero + ps, ps, ops.H(q))) assert not schedule.exclude(ScheduledOperation(zero, ps, ops.X(q))) assert schedule.query(time=zero, duration=ps * 10) == [op] assert schedule.exclude(ScheduledOperation(zero, ps, ops.H(q))) assert schedule.query(time=zero, duration=ps * 10) == [] assert not schedule.exclude(ScheduledOperation(zero, ps, ops.H(q)))
def test_moment_by_moment_schedule_moment_of_two_qubit_ops(): device = _TestDevice() qubits = device.qubits circuit = Circuit( [Moment((ops.CZ(qubits[i], qubits[i + 1]) for i in range(0, 9, 3)))]) schedule = moment_by_moment_schedule(device, circuit) zero_ns = Timestamp() expected = set( ScheduledOperation.op_at_on(ops.CZ(qubits[i], qubits[i + 1]), zero_ns, device) for i in range(0, 9, 3)) assert set(schedule.scheduled_operations) == expected
def test_moment_by_moment_schedule_moment_of_single_qubit_ops(): device = _TestDevice() qubits = device.qubits circuit = Circuit([ Moment(ops.H(q) for q in qubits), ]) schedule = moment_by_moment_schedule(device, circuit) zero_ns = Timestamp() assert set(schedule.scheduled_operations) == { ScheduledOperation.op_at_on(ops.H(q), zero_ns, device) for q in qubits }
def test_include(): q0 = ops.QubitId() q1 = ops.QubitId() zero = Timestamp(picos=0) ps = Duration(picos=1) schedule = Schedule(device=UnconstrainedDevice) op0 = ScheduledOperation(zero, ps, ops.H(q0)) schedule.include(op0) with pytest.raises(ValueError): schedule.include(ScheduledOperation(zero, ps, ops.H(q0))) schedule.include(ScheduledOperation(zero + 0.5 * ps, ps, ops.H(q0))) op1 = ScheduledOperation(zero + 2 * ps, ps, ops.H(q0)) schedule.include(op1) op2 = ScheduledOperation(zero + 0.5 * ps, ps, ops.H(q1)) schedule.include(op2) assert schedule.query(time=zero, duration=ps * 10) == [op0, op2, op1]
def schedule_from_proto( device: XmonDevice, ops: Iterable[operations_pb2.Operation], ) -> Schedule: """Convert protobufs into a Schedule for the given device.""" scheduled_ops = [] last_time_picos = 0 for op in ops: time_picos = last_time_picos + op.incremental_delay_picoseconds last_time_picos = time_picos xmon_op = xmon_gates.XmonGate.from_proto(op) scheduled_ops.append( ScheduledOperation.op_at_on( operation=xmon_op, time=Timestamp(picos=time_picos), device=device, )) return Schedule(device, scheduled_ops)
def test_query_overlapping_operations_exclusive(): q = ops.QubitId() zero = Timestamp(picos=0) ps = Duration(picos=1) op1 = ScheduledOperation(zero, 2 * ps, ops.H(q)) op2 = ScheduledOperation(zero + ps, 2 * ps, ops.H(q)) schedule = 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 schedule_from_proto_dicts( device: XmonDevice, ops: Iterable[Dict], ) -> Schedule: """Convert proto dictionaries into a Schedule for the given device.""" scheduled_ops = [] last_time_picos = 0 for op in ops: delay_picos = 0 if 'incremental_delay_picoseconds' in op: delay_picos = op['incremental_delay_picoseconds'] time_picos = last_time_picos + delay_picos last_time_picos = time_picos xmon_op = xmon_gates.XmonGate.from_proto_dict(op) scheduled_ops.append(ScheduledOperation.op_at_on( operation=xmon_op, time=Timestamp(picos=time_picos), device=device, )) return Schedule(device, scheduled_ops)
def test_slice_operations(): q0 = ops.QubitId() q1 = ops.QubitId() zero = Timestamp(picos=0) ps = Duration(picos=1) op1 = ScheduledOperation(zero, ps, ops.H(q0)) op2 = ScheduledOperation(zero + 2 * ps, 2 * ps, ops.CZ(q0, q1)) op3 = ScheduledOperation(zero + 10 * ps, ps, ops.H(q1)) schedule = Schedule(device=UnconstrainedDevice, scheduled_operations=[op1, op2, op3]) assert schedule[zero] == [op1] assert schedule[zero + ps*0.5] == [op1] assert schedule[zero:zero] == [] assert schedule[zero + ps*0.5:zero + ps*0.5] == [op1] assert schedule[zero:zero + ps] == [op1] assert schedule[zero:zero + 2 * ps] == [op1] assert schedule[zero:zero + 2.1 * ps] == [op1, op2] assert schedule[zero:zero + 20 * ps] == [op1, op2, op3] assert schedule[zero + 2.5 * ps:zero + 20 * ps] == [op2, op3] assert schedule[zero + 5 * ps:zero + 20 * ps] == [op3]
def test_repr(): a = ScheduledOperation(time=Timestamp(picos=5), duration=timedelta(microseconds=7), operation=cirq.H(cirq.NamedQubit('a'))) cirq.testing.assert_equivalent_repr(a)