コード例 #1
0
def test_parametric_gates1():
    for _ in range(REPS):
        theta = random.uniform(-4 * pi, +4 * pi)
        assert qf.almost_unitary(qf.RX(theta))
        assert qf.almost_unitary(qf.RY(theta))
        assert qf.almost_unitary(qf.RZ(theta))

    for _ in range(REPS):
        theta = random.uniform(-4 * pi, +4 * pi)
        assert qf.almost_unitary(qf.TX(theta))
        assert qf.almost_unitary(qf.TY(theta))
        assert qf.almost_unitary(qf.TZ(theta))

    for _ in range(REPS):
        theta = random.uniform(-4 * pi, +4 * pi)
        assert qf.almost_unitary(qf.CPHASE00(theta))
        assert qf.almost_unitary(qf.CPHASE01(theta))
        assert qf.almost_unitary(qf.CPHASE10(theta))
        assert qf.almost_unitary(qf.CPHASE(theta))
        assert qf.almost_unitary(qf.PSWAP(theta))

    assert qf.gates_close(qf.I(), qf.I())
    assert qf.gates_close(qf.RX(pi), qf.X())
    assert qf.gates_close(qf.RY(pi), qf.Y())
    assert qf.gates_close(qf.RZ(pi), qf.Z())
コード例 #2
0
def test_RZZ() -> None:
    theta = 0.23
    gate0 = qf.RZZ(theta, 0, 1)
    gate1 = qf.ZZ(theta / np.pi, 0, 1)
    assert qf.gates_close(gate0, gate1)
    assert qf.gates_close(gate0.H, gate1.H)
    assert qf.gates_close(gate0**0.12, gate1**0.12)
コード例 #3
0
def test_gate_mul() -> None:
    # three cnots same as one swap
    gate0 = qf.IdentityGate([0, 1])

    gate1 = qf.CNot(1, 0)
    gate2 = qf.CNot(0, 1)
    gate3 = qf.CNot(1, 0)

    gate = gate1 @ gate0
    gate = gate2 @ gate
    gate = gate3 @ gate
    assert qf.gates_close(gate, qf.Swap(0, 1))

    # Again, but with labels
    gate0 = qf.IdentityGate(["a", "b"])

    gate1 = qf.CNot("b", "a")
    gate2 = qf.CNot("a", "b")
    gate3 = qf.CNot("b", "a")

    gate = gate1 @ gate0
    gate = gate2 @ gate
    gate = gate3 @ gate
    assert qf.gates_close(gate, qf.Swap("a", "b"))

    gate4 = qf.X("a")
    _ = gate4 @ gate

    with pytest.raises(NotImplementedError):
        _ = gate4 @ 3  # type: ignore
コード例 #4
0
def test_CH() -> None:
    gate1 = qf.CH(0, 1)

    # I picked up this circuit for a CH gate from qiskit
    # qiskit/extensions/standard/ch.py
    # But it clearly far too long. CH is locally equivalent to CNOT,
    # so requires only one CNOT gate.
    circ2 = qf.Circuit([
        qf.H(1),
        qf.S_H(1),
        qf.CNot(0, 1),
        qf.H(1),
        qf.T(1),
        qf.CNot(0, 1),
        qf.T(1),
        qf.H(1),
        qf.S(1),
        qf.X(1),
        qf.S(0),
    ])
    assert qf.gates_close(gate1, circ2.asgate())

    # Here's a better decomposition
    circ1 = qf.Circuit([qf.YPow(+0.25, 1), qf.CNot(0, 1), qf.YPow(-0.25, 1)])
    assert qf.gates_close(gate1, circ1.asgate())
    assert qf.circuits_close(circ1, circ2)
コード例 #5
0
def test_gatemul():
    # three cnots same as one swap
    gate0 = qf.identity_gate([0, 1])

    gate1 = qf.CNOT(1, 0)
    gate2 = qf.CNOT(0, 1)
    gate3 = qf.CNOT(1, 0)

    gate = gate0
    gate = gate1 @ gate
    gate = gate2 @ gate
    gate = gate3 @ gate
    assert qf.gates_close(gate, qf.SWAP())

    # Again, but with labels
    gate0 = qf.identity_gate(['a', 'b'])

    gate1 = qf.CNOT('b', 'a')
    gate2 = qf.CNOT('a', 'b')
    gate3 = qf.CNOT('b', 'a')

    gate = gate0
    gate = gate1 @ gate
    gate = gate2 @ gate
    gate = gate3 @ gate
    assert qf.gates_close(gate, qf.SWAP('a', 'b'))

    gate4 = qf.X('a')
    gate = gate4 @ gate

    with pytest.raises(NotImplementedError):
        gate = gate4 @ 3
コード例 #6
0
def test_gate_angle():
    gate0 = qf.random_gate(1)
    gate1 = qf.random_gate(1)
    qf.gate_angle(gate0, gate1)

    assert not qf.gates_close(gate0, gate1)
    assert qf.gates_close(gate0, gate0)
コード例 #7
0
def test_stdgates(gatet: Type[qf.StdGate]) -> None:

    # Test creation
    gate = _randomize_gate(gatet)

    # Test correct number of qubits
    assert gate.qubit_nb == gatet.cv_qubit_nb

    # Test hermitian conjugate
    inv_gate = gate.H
    gate.tensor
    inv_gate.tensor

    # Test inverse
    eye = gate @ inv_gate
    assert qf.gates_close(qf.IdentityGate(range(gate.qubit_nb)), eye)
    assert qf.gates_phase_close(qf.IdentityGate(range(gate.qubit_nb)), eye)

    # Test pow
    assert qf.gates_close(gate ** -1, inv_gate)
    assert qf.gates_close((gate ** 0.5) ** 2, gate)
    assert qf.gates_close((gate ** 0.3) @ (gate ** 0.7), gate)

    hgate = qf.Unitary((gate ** 0.5).tensor, gate.qubits)
    assert qf.gates_close(hgate @ hgate, gate)
コード例 #8
0
def test_gate_angle() -> None:
    gate0 = qf.RandomGate([1])
    gate1 = qf.RandomGate([1])
    qf.gate_angle(gate0, gate1)

    assert not qf.gates_close(gate0, gate1)
    assert qf.gates_close(gate0, gate0)
コード例 #9
0
def test_cphase_gates():
    for _ in range(REPS):
        theta = random.uniform(-4 * pi, +4 * pi)

        gate11 = qf.control_gate(0, qf.PHASE(theta, 1))
        assert qf.gates_close(gate11, qf.CPHASE(theta, 0, 1))

        gate01 = qf.conditional_gate(0, qf.PHASE(theta, 1), qf.I(1))
        assert qf.gates_close(gate01, qf.CPHASE01(theta))

        gate00 = qf.identity_gate(2)
        gate00 = qf.X(0) @ gate00
        gate00 = qf.X(1) @ gate00
        gate00 = gate11 @ gate00
        gate00 = qf.X(0) @ gate00
        gate00 = qf.X(1) @ gate00
        assert qf.gates_close(gate00, qf.CPHASE00(theta))

        gate10 = qf.identity_gate(2)
        gate10 = qf.X(0) @ gate10
        gate10 = qf.X(1) @ gate10
        gate10 = gate01 @ gate10
        gate10 = qf.X(0) @ gate10
        gate10 = qf.X(1) @ gate10
        assert qf.gates_close(gate10, qf.CPHASE10(theta))
コード例 #10
0
def test_pswap():
    for _ in range(REPS):
        theta = random.uniform(-4 * pi, +4 * pi)
        assert qf.almost_unitary(qf.PSWAP(theta))

    assert qf.gates_close(qf.SWAP(), qf.PSWAP(0))

    assert qf.gates_close(qf.ISWAP(), qf.PSWAP(pi / 2))
コード例 #11
0
def test_CV() -> None:
    gate0 = qf.CNot(0, 1)**0.5
    gate1 = qf.CV(0, 1)
    assert qf.gates_close(gate0, gate1)

    gate2 = qf.CV_H(0, 1)
    assert qf.gates_close(gate0.H, gate2)

    assert qf.gates_close(gate1.H.H, gate1)
コード例 #12
0
def test_gate_permute():
    gate0 = qf.CNOT(0, 1)
    gate1 = qf.CNOT(1, 0)

    assert not qf.gates_close(gate0, gate1)

    gate2 = gate1.permute([0, 1])
    assert gate2.qubits == (0, 1)
    assert qf.gates_close(gate1, gate2)
コード例 #13
0
def test_can_to_cnot() -> None:
    gate = qf.Can(0.3, 0.23, 0.22, 0, 1)
    circ = qf.Circuit(qf.translate_can_to_cnot(gate))  # type: ignore
    assert qf.gates_close(gate, circ.asgate())

    gate = qf.Can(0.3, 0.23, 0.0, 0, 1)
    circ = qf.Circuit(qf.translate_can_to_cnot(gate))  # type: ignore
    print(qf.canonical_decomposition(circ.asgate()))
    assert qf.gates_close(gate, circ.asgate())
コード例 #14
0
def test_circuit_to_qutip() -> None:
    q0, q1, q2 = 0, 1, 2

    circ0 = qf.Circuit()
    circ0 += qf.I(q0)
    circ0 += qf.Ph(0.1, q0)
    circ0 += qf.X(q0)
    circ0 += qf.Y(q1)

    circ0 += qf.Z(q0)
    circ0 += qf.S(q1)
    circ0 += qf.T(q2)

    circ0 += qf.H(q0)
    circ0 += qf.H(q1)
    circ0 += qf.H(q2)

    circ0 += qf.CNot(q0, q1)
    circ0 += qf.CNot(q1, q0)
    circ0 += qf.Swap(q0, q1)
    circ0 += qf.ISwap(q0, q1)

    circ0 += qf.CCNot(q0, q1, q2)
    circ0 += qf.CSwap(q0, q1, q2)

    circ0 == qf.I(q0)
    circ0 += qf.Rx(0.1, q0)
    circ0 += qf.Ry(0.2, q1)
    circ0 += qf.Rz(0.3, q2)
    circ0 += qf.V(q0)
    circ0 += qf.H(q1)
    circ0 += qf.CY(q0, q1)
    circ0 += qf.CZ(q0, q1)

    circ0 += qf.CS(q1, q2)
    circ0 += qf.CT(q0, q1)

    circ0 += qf.SqrtSwap(q0, q1)
    circ0 += qf.SqrtISwap(q0, q1)
    circ0 += qf.CCNot(q0, q1, q2)
    circ0 += qf.CSwap(q0, q1, q2)

    circ0 += qf.CPhase(0.1, q1, q2)

    # Not yet supported
    # circ0 += qf.B(q1, q2)
    # circ0 += qf.Swap(q1, q2) ** 0.1

    qbc = xqutip.circuit_to_qutip(circ0)
    U = gate_sequence_product(qbc.propagators())
    gate0 = qf.Unitary(U.full(), qubits=[0, 1, 2])
    assert qf.gates_close(gate0, circ0.asgate())

    circ1 = xqutip.qutip_to_circuit(qbc)

    assert qf.gates_close(circ0.asgate(), circ1.asgate())
コード例 #15
0
def test_gate_inverse():
    inv = qf.S().H

    eye = qf.S() @ inv
    assert qf.gates_close(eye, qf.I())

    inv = qf.ISWAP().H
    eye = qf.ISWAP() @ inv

    assert qf.gates_close(eye, qf.identity_gate(2))
コード例 #16
0
def test_EXCH():
    t = random.uniform(-2, +2)
    gate = qf.EXCH(t)
    assert qf.almost_unitary(gate)
    inv = gate.H
    assert type(gate) == type(inv)
    assert qf.gates_close(qf.identity_gate(2), inv @ gate)

    gate1 = qf.CANONICAL(t, t, t)
    assert qf.gates_close(gate, gate1)
コード例 #17
0
def test_piswap():
    for _ in range(REPS):
        theta = random.uniform(-4 * pi, +4 * pi)
        assert qf.almost_unitary(qf.PISWAP(theta))

    for _ in range(REPS):
        theta = random.uniform(0, +pi)

    assert qf.gates_close(qf.PISWAP(0), qf.identity_gate(2))

    assert qf.gates_close(qf.PISWAP(pi / 4), qf.ISWAP())
コード例 #18
0
def test_CNotPow() -> None:
    q0, q1 = 2, 3
    for _ in range(REPS):
        t = random.uniform(-4, +4)
        gate0 = qf.CNotPow(t, q0, q1)
        gate1 = qf.ControlGate([q0], qf.XPow(t, q1))
        gate2 = qf.CNot(q0, q1)**t
        gate3 = qf.CNotPow(1, q0, q1)**t

        assert qf.gates_close(gate0, gate1)
        assert qf.gates_close(gate0, gate2)
        assert qf.gates_close(gate0, gate3)
コード例 #19
0
def test_inverse_1qubit():
    # These gate pairs are inverses of each other
    gate_names = [('S', 'S_H'), ('T', 'T_H')]

    for name0, name1 in gate_names:
        gate0 = qf.STDGATES[name0]()
        gate1 = qf.STDGATES[name1]()
        assert qf.gates_close(gate0, gate1.H)
        assert qf.gates_close(gate0.H, gate1)

        assert qf.gates_close(gate0, gate1**-1)
        assert qf.gates_close(gate0**-1, gate1)
コード例 #20
0
def test_PauliGate_pow() -> None:
    alpha = 0.4
    gate0 = qf.PauliGate(qf.sZ(0), alpha)
    gate1 = gate0 ** 0.3
    gate2 = qf.Unitary(gate0.tensor, gate0.qubits) ** 0.3
    assert qf.gates_close(gate1, gate2)
    assert qf.gates_close(gate1.H, gate2 ** -1)

    gate3 = qf.unitary_from_hamiltonian(gate0.hamiltonian, qubits=gate0.qubits)
    assert qf.gates_close(gate0, gate3)

    s = str(gate0)
    print(s)
コード例 #21
0
def test_gate_permute() -> None:
    gate0 = qf.CNot(0, 1)
    gate1 = qf.CNot(1, 0)

    assert not qf.gates_close(gate0, gate1)

    gate2 = gate1.permute([0, 1])
    assert gate2.qubits == (0, 1)
    assert qf.gates_close(gate1, gate2)

    gate3 = qf.ISwap(0, 1)
    gate4 = gate3.permute([1, 0])
    assert qf.gates_close(gate3, gate4)
コード例 #22
0
def test_rn() -> None:
    theta = 1.23

    gate = qf.Rn(theta, 1, 0, 0, "q0")
    assert qf.gates_close(gate, qf.Rx(theta, "q0"))

    gate = qf.Rn(theta, 0, 1, 0, "q0")
    assert qf.gates_close(gate, qf.Ry(theta, "q0"))

    gate = qf.Rn(theta, 0, 0, 1, "q0")
    assert qf.gates_close(gate, qf.Rz(theta, "q0"))

    gate = qf.Rn(np.pi, 1 / np.sqrt(2), 0, 1 / np.sqrt(2), "q0")
    assert qf.gates_close(gate, qf.H("q0"))
コード例 #23
0
def test_FSim() -> None:
    for _ in range(REPS):
        theta = random.uniform(-np.pi, +np.pi)
        phi = random.uniform(-np.pi, +np.pi)
        gate0 = qf.FSim(theta, phi, 0, 1)

        # Test with decomposition from Cirq.
        circ = qf.Circuit()
        circ += qf.XX(theta / np.pi, 0, 1)
        circ += qf.YY(theta / np.pi, 0, 1)
        circ += qf.CZ(0, 1)**(-phi / np.pi)
        gate1 = circ.asgate()
        assert qf.gates_close(gate0, gate1)

        assert qf.gates_close(gate1.H, gate0.H)
コード例 #24
0
def test_canonical_decomposition() -> None:
    for tt1 in range(0, 6):
        for tt2 in range(tt1):
            for tt3 in range(tt2):
                t1, t2, t3 = tt1 / 12, tt2 / 12, tt3 / 12
                if t3 == 0 and t1 > 0.5:
                    continue
                coords = np.asarray((t1, t2, t3))

                circ0 = qf.Circuit()
                circ0 += qf.RandomGate([0])
                circ0 += qf.RandomGate([1])
                circ0 += qf.Can(t1, t2, t3, 0, 1)
                circ0 += qf.RandomGate([0])
                circ0 += qf.RandomGate([1])
                gate0 = circ0.asgate()

                circ1 = qf.canonical_decomposition(gate0)
                assert qf.gates_close(gate0, circ1.asgate())

                canon = circ1[1]
                new_coords = np.asarray(
                    [canon.param(n) for n in ["tx", "ty", "tz"]])
                assert np.allclose(coords, np.asarray(new_coords))

                coords2 = qf.canonical_coords(gate0)
                assert np.allclose(coords, np.asarray(coords2))
コード例 #25
0
def test_euler_decomposition() -> None:
    gate0 = qf.RandomGate([1])

    for order in ["XYX", "XZX", "YXY", "YZY", "ZXZ", "ZYZ"]:
        circ1 = qf.euler_decomposition(gate0, euler=order)
        gate1 = circ1.asgate()
        assert qf.gates_close(gate0, gate1)
コード例 #26
0
def test_parametric_TX_TY_TZ():
    gate = qf.I()
    gate = qf.TZ(1 / 2) @ gate
    gate = qf.TX(1 / 2) @ gate
    gate = qf.TZ(1 / 2) @ gate

    assert qf.gates_close(gate, qf.H())
コード例 #27
0
def test_canonical_decomposition():
    for tt1 in range(0, 10):
        for tt2 in range(tt1):
            for tt3 in range(tt2):
                t1, t2, t3 = tt1 / 20, tt2 / 20, tt3 / 20
                if t3 == 0 and t1 > 0.5:
                    continue
                coords = np.asarray((t1, t2, t3))

                print('b')
                circ0 = qf.Circuit()
                circ0 += qf.ZYZ(0.2, 0.2, 0.2, q0=0)
                circ0 += qf.ZYZ(0.3, 0.3, 0.3, q0=1)
                circ0 += qf.CANONICAL(t1, t2, t3, 0, 1)
                circ0 += qf.ZYZ(0.15, 0.2, 0.3, q0=0)
                circ0 += qf.ZYZ(0.15, 0.22, 0.3, q0=1)
                gate0 = circ0.asgate()
                print('c')

                circ1 = qf.canonical_decomposition(gate0)
                assert qf.gates_close(gate0, circ1.asgate())
                print('d')

                print(circ1)
                canon = circ1.elements[6]
                new_coords = np.asarray(
                    [canon.params[n] for n in ['tx', 'ty', 'tz']])
                assert np.allclose(coords, np.asarray(new_coords))

                coords2 = qf.canonical_coords(gate0)
                assert np.allclose(coords, np.asarray(coords2))
                print('>')
                print()
コード例 #28
0
def test_stdgates_repr(gatet: Type[qf.StdGate]) -> None:

    gate0 = _randomize_gate(gatet)
    rep = repr(gate0)
    gate1 = eval(rep, qf.StdGate.cv_stdgates)
    assert type(gate0) == type(gate1)
    assert qf.gates_close(gate0, gate1)
コード例 #29
0
def test_inverse_random():
    K = 4
    for _ in range(REPS):
        gate = qf.random_gate(K)
        inv = gate.H
        gate = inv @ gate
        assert qf.gates_close(qf.identity_gate(4), gate)
コード例 #30
0
def test_cirq_to_circuit_0_7() -> None:
    q0 = cq.LineQubit(0)
    q1 = cq.LineQubit(1)
    gate = cirq_to_circuit(cq.Circuit(cq.rx(0.5).on(q0)))[0]
    assert isinstance(gate, qf.XPow)
    assert gate.param("t") == 0.5 / pi

    gate = cirq_to_circuit(cq.Circuit(cq.ry(0.5).on(q0)))[0]
    assert isinstance(gate, qf.YPow)
    assert gate.param("t") == 0.5 / pi

    gate = cirq_to_circuit(cq.Circuit(cq.rz(0.5).on(q0)))[0]
    assert isinstance(gate, qf.ZPow)
    assert gate.param("t") == 0.5 / pi

    # gate = cirq_to_circuit(cq.Circuit(cq.IdentityGate(2).on(q0, q1)))[0]

    op = (cq.PhasedISwapPowGate()**0.5).on(q0, q1)
    circ = cirq_to_circuit(cq.Circuit(op))
    assert qf.gates_close(circ.asgate(), qf.Givens(0.5 * pi / 2, 0, 1))

    op = cq.PhasedXZGate(x_exponent=0.125,
                         z_exponent=0.25,
                         axis_phase_exponent=0.375).on(q0)
    circ = cirq_to_circuit(cq.Circuit(op))
    assert len(circ) == 3