Exemplo n.º 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())
Exemplo n.º 2
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))
Exemplo n.º 3
0
def test_gatepow():
    gates = [
        qf.I(),
        qf.X(),
        qf.Y(),
        qf.Z(),
        qf.H(),
        qf.S(),
        qf.T(),
        qf.PHASE(0.1),
        qf.RX(0.2),
        qf.RY(0.3),
        qf.RZ(0.4),
        qf.CZ(),
        qf.CNOT(),
        qf.SWAP(),
        qf.ISWAP(),
        qf.CPHASE00(0.5),
        qf.CPHASE01(0.6),
        qf.CPHASE10(0.6),
        qf.CPHASE(0.7),
        qf.PSWAP(0.15),
        qf.CCNOT(),
        qf.CSWAP(),
        qf.TX(2.7),
        qf.TY(1.2),
        qf.TZ(0.3),
        qf.ZYZ(3.5, 0.9, 2.1),
        qf.CANONICAL(0.1, 0.2, 7.4),
        qf.XX(1.8),
        qf.YY(0.9),
        qf.ZZ(0.45),
        qf.PISWAP(0.2),
        qf.EXCH(0.1),
        qf.TH(0.3)
    ]

    for gate in gates:
        assert qf.gates_close(gate.H, gate**-1)

    for gate in gates:
        sqrt_gate = gate**(1 / 2)
        two_gate = sqrt_gate @ sqrt_gate
        assert qf.gates_close(gate, two_gate)

    for gate in gates:
        gate0 = gate**0.3
        gate1 = gate**0.7
        gate2 = gate0 @ gate1
        assert qf.gates_close(gate, gate2)

    for K in range(1, 5):
        gate = qf.random_gate(K)  # FIXME: Throw error on K=0
        sqrt_gate = gate**0.5
        two_gate = sqrt_gate @ sqrt_gate
        assert qf.gates_close(gate, two_gate)

    for gate in gates:
        rgate = qf.Gate((gate**0.5).tensor)
        tgate = rgate @ rgate
        assert qf.gates_close(gate, tgate)