Beispiel #1
0
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)
Beispiel #2
0
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)
Beispiel #3
0
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)
Beispiel #4
0
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 = cirq.Timestamp()
    twenty_ns = cirq.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),
    }
Beispiel #5
0
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
Beispiel #6
0
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 = cirq.Timestamp()
    fourty_ns = cirq.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),
    }
Beispiel #7
0
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 = cirq.Timestamp()
    twenty_ns = cirq.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
Beispiel #8
0
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(Schedule(device, []))

    device.validate_schedule(
        Schedule(device, [
            ScheduledOperation.op_at_on(cirq.H(cirq.LineQubit(0)),
                                        cirq.Timestamp(), device),
            ScheduledOperation.op_at_on(
                cirq.CZ(cirq.LineQubit(0), cirq.LineQubit(1)),
                cirq.Timestamp(), device)
        ]))

    with pytest.raises(ValueError):
        device.validate_schedule(
            Schedule(device, [
                ScheduledOperation.op_at_on(NotImplementedOperation(),
                                            cirq.Timestamp(), device)
            ]))

    with pytest.raises(ValueError):
        device.validate_schedule(
            Schedule(device, [
                ScheduledOperation.op_at_on(cirq.X(cirq.LineQubit(0)),
                                            cirq.Timestamp(), device)
            ]))

    with pytest.raises(ValueError):
        device.validate_schedule(
            Schedule(device, [
                ScheduledOperation.op_at_on(cirq.H(cirq.NamedQubit('q')),
                                            cirq.Timestamp(), device)
            ]))

    with pytest.raises(ValueError):
        device.validate_schedule(
            Schedule(device, [
                ScheduledOperation.op_at_on(cirq.H(cirq.LineQubit(100)),
                                            cirq.Timestamp(), device)
            ]))

    with pytest.raises(ValueError):
        device.validate_schedule(
            Schedule(device, [
                ScheduledOperation.op_at_on(
                    cirq.CZ(cirq.LineQubit(1), cirq.LineQubit(3)),
                    cirq.Timestamp(), device)
            ]))
Beispiel #9
0
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 = cirq.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
Beispiel #10
0
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 = cirq.Timestamp()
    assert set(schedule.scheduled_operations) == {
        ScheduledOperation.op_at_on(ops.H(q), zero_ns, device)
        for q in qubits
    }
Beispiel #11
0
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)
Beispiel #12
0
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)
Beispiel #13
0
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)