Exemple #1
0
def test_equality():
    a = ops.QubitId()
    b = ops.QubitId()
    c = ops.QubitId()
    d = ops.QubitId()

    eq = EqualsTester()

    # Default is empty. Iterables get frozen into tuples.
    eq.add_equality_group(Moment(),
                          Moment([]), Moment(()))
    eq.add_equality_group(
        Moment([ops.X(d)]), Moment((ops.X(d),)))

    # Equality depends on gate and qubits.
    eq.add_equality_group(Moment([ops.X(a)]))
    eq.add_equality_group(Moment([ops.X(b)]))
    eq.add_equality_group(Moment([ops.Y(a)]))

    # Equality depends on order.
    eq.add_equality_group(Moment([ops.X(a), ops.X(b)]))
    eq.add_equality_group(Moment([ops.X(b), ops.X(a)]))

    # Two qubit gates.
    eq.make_equality_pair(lambda: Moment([ops.CZ(c, d)]))
    eq.make_equality_pair(lambda: Moment([ops.CZ(a, c)]))
    eq.make_equality_pair(lambda: Moment([ops.CZ(a, b), ops.CZ(c, d)]))
    eq.make_equality_pair(lambda: Moment([ops.CZ(a, c), ops.CZ(b, d)]))
Exemple #2
0
def test_qubits():
    a = ops.QubitId()
    b = ops.QubitId()

    assert Moment([ops.X(a), ops.X(b)]).qubits == {a , b}
    assert Moment([ops.X(a)]).qubits == {a}
    assert Moment([ops.CZ(a, b)]).qubits == {a, b}
Exemple #3
0
def test_depolarizer_different_gate():
    q1 = ops.QubitId()
    q2 = ops.QubitId()
    cnot = Job(circuits.Circuit([
        circuits.Moment([ops.CNOT(q1, q2)]),
    ]))
    allerrors = DepolarizerChannel(
        probability=1.0,
        depolarizing_gates=[xmon_gates.ExpZGate(),
                            xmon_gates.ExpWGate()])
    p0 = Symbol(DepolarizerChannel._parameter_name + '0')
    p1 = Symbol(DepolarizerChannel._parameter_name + '1')
    p2 = Symbol(DepolarizerChannel._parameter_name + '2')
    p3 = Symbol(DepolarizerChannel._parameter_name + '3')

    error_sweep = (Points(p0.name, [1.0]) + Points(p1.name, [1.0]) +
                   Points(p2.name, [1.0]) + Points(p3.name, [1.0]))

    cnot_then_z = Job(
        circuits.Circuit([
            circuits.Moment([ops.CNOT(q1, q2)]),
            circuits.Moment([
                xmon_gates.ExpZGate(half_turns=p0).on(q1),
                xmon_gates.ExpZGate(half_turns=p1).on(q2)
            ]),
            circuits.Moment([
                xmon_gates.ExpWGate(half_turns=p2).on(q1),
                xmon_gates.ExpWGate(half_turns=p3).on(q2)
            ])
        ]), cnot.sweep * error_sweep)

    assert allerrors.transform_job(cnot) == cnot_then_z
Exemple #4
0
def test_copy():
    a = ops.QubitId()
    b = ops.QubitId()
    original = Moment([ops.CZ(a, b)])
    copy = original.__copy__()
    assert original == copy
    assert id(original) != id(copy)
Exemple #5
0
def test_controlled_op_to_gates_omits_negligible_global_phase():
    qc = ops.QubitId()
    qt = ops.QubitId()
    operations = decompositions.controlled_op_to_native_gates(
        control=qc, target=qt, operation=ops.H.matrix(), tolerance=0.0001)

    assert operations == [ops.Y(qt)**-0.25, ops.CZ(qc, qt), ops.Y(qt)**0.25]
Exemple #6
0
def test_operates_on():
    a = ops.QubitId()
    b = ops.QubitId()
    c = ops.QubitId()

    # Empty case.
    assert not Moment().operates_on([])
    assert not Moment().operates_on([a])
    assert not Moment().operates_on([b])
    assert not Moment().operates_on([a, b])

    # One-qubit operation case.
    assert not Moment([ops.X(a)]).operates_on([])
    assert Moment([ops.X(a)]).operates_on([a])
    assert not Moment([ops.X(a)]).operates_on([b])
    assert Moment([ops.X(a)]).operates_on([a, b])

    # Two-qubit operation case.
    assert not Moment([ops.CZ(a, b)]).operates_on([])
    assert Moment([ops.CZ(a, b)]).operates_on([a])
    assert Moment([ops.CZ(a, b)]).operates_on([b])
    assert Moment([ops.CZ(a, b)]).operates_on([a, b])
    assert not Moment([ops.CZ(a, b)]).operates_on([c])
    assert Moment([ops.CZ(a, b)]).operates_on([a, c])
    assert Moment([ops.CZ(a, b)]).operates_on([a, b, c])

    # Multiple operations case.
    assert not Moment([ops.X(a), ops.X(b)]).operates_on([])
    assert Moment([ops.X(a), ops.X(b)]).operates_on([a])
    assert Moment([ops.X(a), ops.X(b)]).operates_on([b])
    assert Moment([ops.X(a), ops.X(b)]).operates_on([a, b])
    assert not Moment([ops.X(a), ops.X(b)]).operates_on([c])
    assert Moment([ops.X(a), ops.X(b)]).operates_on([a, c])
    assert Moment([ops.X(a), ops.X(b)]).operates_on([a, b, c])
Exemple #7
0
def test_trivial_parity_interaction_corner_case():
    q0 = ops.QubitId()
    q1 = ops.QubitId()
    nearPi4 = np.pi / 4 * 0.99
    tolerance = 1e-2
    circuit = circuits.Circuit.from_ops(
        decompositions._parity_interaction(q0, q1, -nearPi4, tolerance))
    assert len(circuit) == 2
Exemple #8
0
def test_controlled_op_to_gates_equivalent_on_known_and_random(mat):
    qc = ops.QubitId()
    qt = ops.QubitId()
    operations = decompositions.controlled_op_to_native_gates(control=qc,
                                                              target=qt,
                                                              operation=mat)
    actual_effect = _operations_to_matrix(operations, (qc, qt))
    intended_effect = linalg.kron_with_controls(linalg.CONTROL_TAG, mat)
    assert linalg.allclose_up_to_global_phase(actual_effect, intended_effect)
def test_clears_paired_cnot():
    q0 = ops.QubitId()
    q1 = ops.QubitId()
    assert_optimizes(
        before=circuits.Circuit([
            circuits.Moment([ops.CNOT(q0, q1)]),
            circuits.Moment([ops.CNOT(q0, q1)]),
        ]),
        after=circuits.Circuit())
Exemple #10
0
def test_cnots_separated_by_single_gates_correct():
    q0 = ops.QubitId()
    q1 = ops.QubitId()
    assert_optimization_not_broken(
        circuits.Circuit.from_ops(
            ops.CNOT(q0, q1),
            ops.H(q1),
            ops.CNOT(q0, q1),
        ))
Exemple #11
0
def test_depolarizer_no_errors():
    q1 = ops.QubitId()
    q2 = ops.QubitId()
    cnot = Job(circuits.Circuit([
        circuits.Moment([ops.CNOT(q1, q2)]),
    ]))
    noerrors = DepolarizerChannel(probability=0.0)

    assert noerrors.transform_job(cnot) == cnot
Exemple #12
0
def test_with_operation():
    a = ops.QubitId()
    b = ops.QubitId()

    assert Moment().with_operation(ops.X(a)) == Moment([ops.X(a)])

    assert (Moment([ops.X(a)]).with_operation(ops.X(b)) ==
            Moment([ops.X(a), ops.X(b)]))

    with pytest.raises(ValueError):
        _ = Moment([ops.X(a)]).with_operation(ops.X(a))
Exemple #13
0
def test_ignores_2qubit_target():
    m = MergeRotations(0.000001)
    q = ops.QubitId()
    q2 = ops.QubitId()
    c = circuits.Circuit([
        circuits.Moment([ops.CZ(q, q2)]),
    ])

    m.optimization_at(c, 0, c.operation_at(q, 0))

    assert c == circuits.Circuit([circuits.Moment([ops.CZ(q, q2)])])
Exemple #14
0
def test_drop():
    q1 = ops.QubitId()
    q2 = ops.QubitId()
    assert_optimizes(before=circuits.Circuit([
        circuits.Moment(),
        circuits.Moment(),
        circuits.Moment([ops.CNOT(q1, q2)]),
        circuits.Moment(),
    ]),
                     after=circuits.Circuit([
                         circuits.Moment([ops.CNOT(q1, q2)]),
                     ]))
Exemple #15
0
def test_clears_known_empties_even_at_zero_tolerance():
    m = circuits.DropNegligible(0.001)
    q = ops.QubitId()
    q2 = ops.QubitId()
    c = circuits.Circuit.from_ops(
        ops.Z(q)**0,
        ops.Y(q)**0.0000001,
        ops.X(q)**-0.0000001,
        ops.CZ(q, q2)**0)

    m.optimize_circuit(c)
    assert c == circuits.Circuit([circuits.Moment()] * 4)
Exemple #16
0
def test_two_to_native_equivalent_and_bounded_for_known_and_random(
        max_partial_cz_depth, max_full_cz_depth, effect):
    q0 = ops.QubitId()
    q1 = ops.QubitId()

    operations_with_partial = decompositions.two_qubit_matrix_to_native_gates(
        q0, q1, effect, True)
    operations_with_full = decompositions.two_qubit_matrix_to_native_gates(
        q0, q1, effect, False)

    assert_ops_implement_unitary(q0, q1, operations_with_partial, effect)
    assert_ops_implement_unitary(q0, q1, operations_with_full, effect)

    assert_cz_depth_below(operations_with_partial, max_partial_cz_depth, False)
    assert_cz_depth_below(operations_with_full, max_full_cz_depth, True)
Exemple #17
0
def test_controlled_op_to_gates_concrete_case():
    qc = ops.QubitId()
    qt = ops.QubitId()
    operations = decompositions.controlled_op_to_native_gates(
        control=qc,
        target=qt,
        operation=np.array([[1, 1j], [1j, 1]]) * np.sqrt(0.5),
        tolerance=0.0001)

    assert operations == [
        ops.Y(qt)**-0.5,
        ops.CZ(qc, qt)**1.5,
        ops.Z(qc)**0.25,
        ops.Y(qt)**0.5
    ]
Exemple #18
0
def test_ignores_czs_separated_by_outer_cz():
    q00 = ops.QubitId()
    q01 = ops.QubitId()
    q10 = ops.QubitId()
    assert_optimizes(
        before=circuits.Circuit([
            circuits.Moment([ops.CZ(q00, q01)]),
            circuits.Moment([ops.CZ(q00, q10)]),
            circuits.Moment([ops.CZ(q00, q01)]),
        ]),
        after=circuits.Circuit([
            circuits.Moment([ops.CZ(q00, q01)]),
            circuits.Moment([ops.CZ(q00, q10)]),
            circuits.Moment([ops.CZ(q00, q01)]),
        ]))
Exemple #19
0
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) == []
Exemple #20
0
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) == []
Exemple #21
0
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) == []
Exemple #22
0
def test_ignores_czs_separated_by_parameterized():
    q0 = ops.QubitId()
    q1 = ops.QubitId()
    assert_optimizes(
        before=circuits.Circuit([
            circuits.Moment([ops.CZ(q0, q1)]),
            circuits.Moment([ExpZGate(
                half_turns=Symbol('boo'))(q0)]),
            circuits.Moment([ops.CZ(q0, q1)]),
        ]),
        after=circuits.Circuit([
            circuits.Moment([ops.CZ(q0, q1)]),
            circuits.Moment([ExpZGate(
                half_turns=Symbol('boo'))(q0)]),
            circuits.Moment([ops.CZ(q0, q1)]),
        ]))
Exemple #23
0
def test_without_operations_touching():
    a = ops.QubitId()
    b = ops.QubitId()
    c = ops.QubitId()

    # Empty case.
    assert Moment().without_operations_touching([]) == Moment()
    assert Moment().without_operations_touching([a]) == Moment()
    assert Moment().without_operations_touching([a, b]) == Moment()

    # One-qubit operation case.
    assert (Moment([ops.X(a)]).without_operations_touching([]) ==
            Moment([ops.X(a)]))
    assert (Moment([ops.X(a)]).without_operations_touching([a]) ==
            Moment())
    assert (Moment([ops.X(a)]).without_operations_touching([b]) ==
            Moment([ops.X(a)]))

    # Two-qubit operation case.
    assert (Moment([ops.CZ(a, b)]).without_operations_touching([]) ==
            Moment([ops.CZ(a, b)]))
    assert (Moment([ops.CZ(a, b)]).without_operations_touching([a]) ==
            Moment())
    assert (Moment([ops.CZ(a, b)]).without_operations_touching([b]) ==
            Moment())
    assert (Moment([ops.CZ(a, b)]).without_operations_touching([c]) ==
            Moment([ops.CZ(a, b)]))

    # Multiple operation case.
    assert (Moment([ops.CZ(a, b),
                    ops.X(c)]).without_operations_touching([]) ==
            Moment([ops.CZ(a, b), ops.X(c)]))
    assert (Moment([ops.CZ(a, b),
                    ops.X(c)]).without_operations_touching([a]) ==
            Moment([ops.X(c)]))
    assert (Moment([ops.CZ(a, b),
                    ops.X(c)]).without_operations_touching([b]) ==
            Moment([ops.X(c)]))
    assert (Moment([ops.CZ(a, b),
                    ops.X(c)]).without_operations_touching([c]) ==
            Moment([ops.CZ(a, b)]))
    assert (Moment([ops.CZ(a, b),
                    ops.X(c)]).without_operations_touching([a, b]) ==
            Moment([ops.X(c)]))
    assert (Moment([ops.CZ(a, b),
                    ops.X(c)]).without_operations_touching([a, c]) ==
            Moment())
Exemple #24
0
def test_stopped_at_2qubit():
    m = MergeRotations(0.000001)
    q = ops.QubitId()
    q2 = ops.QubitId()
    c = circuits.Circuit([
        circuits.Moment([ops.Z(q)]),
        circuits.Moment([ops.H(q)]),
        circuits.Moment([ops.X(q)]),
        circuits.Moment([ops.H(q)]),
        circuits.Moment([ops.CZ(q, q2)]),
        circuits.Moment([ops.H(q)]),
    ])

    assert (m.optimization_at(c, 0, c.operation_at(
        q, 0)) == circuits.PointOptimizationSummary(clear_span=4,
                                                    clear_qubits=[q],
                                                    new_operations=[]))
Exemple #25
0
def test_leaves_singleton():
    m = MergeRotations(0.000001)
    q = ops.QubitId()
    c = circuits.Circuit([circuits.Moment([ops.X(q)])])

    m.optimization_at(c, 0, c.operation_at(q, 0))

    assert c == circuits.Circuit([circuits.Moment([ops.X(q)])])
Exemple #26
0
def test_leaves_big():
    m = circuits.DropNegligible(0.001)
    q = ops.QubitId()
    c = circuits.Circuit([circuits.Moment([ops.Z(q)**0.1])])
    assert m.optimization_at(c, 0, c.operation_at(q, 0)) is None

    d = circuits.Circuit(c.moments)
    m.optimize_circuit(d)
    assert d == c
Exemple #27
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]
Exemple #28
0
def test_removes_identity_sequence():
    q = ops.QubitId()
    assert_optimizes(before=circuits.Circuit([
        circuits.Moment([ops.Z(q)]),
        circuits.Moment([ops.H(q)]),
        circuits.Moment([ops.X(q)]),
        circuits.Moment([ops.H(q)]),
    ]),
                     after=circuits.Circuit())
Exemple #29
0
def test_inefficient_circuit_correct():
    t = 0.1
    v = 0.11
    q0 = ops.QubitId()
    q1 = ops.QubitId()
    assert_optimization_not_broken(
        circuits.Circuit.from_ops(
            ops.H(q1),
            ops.CNOT(q0, q1),
            ops.H(q1),
            ops.CNOT(q0, q1),
            ops.CNOT(q1, q0),
            ops.H(q0),
            ops.CNOT(q0, q1),
            ops.Z(q0)**t, ops.Z(q1)**-t,
            ops.CNOT(q0, q1),
            ops.H(q0), ops.Z(q1)**v,
            ops.CNOT(q0, q1),
            ops.Z(q0)**-v, ops.Z(q1)**-v,
        ))
Exemple #30
0
def test_validation():
    a = ops.QubitId()
    b = ops.QubitId()
    c = ops.QubitId()
    d = ops.QubitId()

    _ = Moment([])
    _ = Moment([ops.X(a)])
    _ = Moment([ops.CZ(a, b)])
    _ = Moment([ops.CZ(b, d)])
    _ = Moment([ops.CZ(a, b), ops.CZ(c, d)])
    _ = Moment([ops.CZ(a, c), ops.CZ(b, d)])
    _ = Moment([ops.CZ(a, c), ops.X(b)])

    with pytest.raises(ValueError):
        _ = Moment([ops.X(a), ops.X(a)])
    with pytest.raises(ValueError):
        _ = Moment([ops.CZ(a, c), ops.X(c)])
    with pytest.raises(ValueError):
        _ = Moment([ops.CZ(a, c), ops.CZ(c, d)])