Exemple #1
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
Exemple #2
0
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]