def test_RN():
    for _ in range(REPS):
        theta = random.uniform(-2 * pi, +2 * pi)
        nx = random.uniform(0, 1)
        ny = random.uniform(0, 1)
        nz = random.uniform(0, 1)
        L = np.sqrt(nx**2 + ny**2 + nz**2)
        nx /= L
        ny /= L
        nz /= L
        gate = qf.RN(theta, nx, ny, nz)
        assert qf.almost_unitary(gate)

        gate2 = qf.RN(-theta, nx, ny, nz)
        assert qf.gates_close(gate.H, gate2)
        assert qf.gates_close(gate**-1, gate2)

    theta = 1.23

    gate = qf.RN(theta, 1, 0, 0)
    assert qf.gates_close(gate, qf.RX(theta))

    gate = qf.RN(theta, 0, 1, 0)
    assert qf.gates_close(gate, qf.RY(theta))

    gate = qf.RN(theta, 0, 0, 1)
    assert qf.gates_close(gate, qf.RZ(theta))

    gate = qf.RN(pi, np.sqrt(2), 0, np.sqrt(2))
    assert qf.gates_close(gate, qf.H())
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))
def test_XX_YY_ZZ():
    gates = [qf.XX, qf.YY, qf.ZZ]
    for gate_class in gates:
        t = random.uniform(-2, +2)
        gate = gate_class(t)
        assert qf.almost_unitary(gate)
        inv = gate.H
        assert type(gate) == type(inv)
        assert qf.gates_close(qf.identity_gate(2), gate @ inv)
def test_CAN():
    t1 = random.uniform(-2, +2)
    t2 = random.uniform(-2, +2)
    t3 = random.uniform(-2, +2)
    gate = qf.CAN(t1, t2, t3)
    assert qf.almost_unitary(gate)
    inv = gate.H
    assert type(gate) == type(inv)
    assert qf.gates_close(qf.identity_gate(2), inv @ gate)
def test_ZYZ():
    t0 = random.uniform(-2, +2)
    t1 = random.uniform(-2, +2)
    t2 = random.uniform(-2, +2)

    gate = qf.ZYZ(t0, t1, t2, 0)
    assert qf.almost_unitary(gate)
    inv = gate.H
    assert type(gate) == type(inv)
    assert qf.gates_close(qf.I(0), inv @ gate)
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)
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())
def test_unitary_1qubit():
    assert qf.almost_unitary(qf.X())
    assert qf.almost_unitary(qf.Y())
    assert qf.almost_unitary(qf.Z())
    assert qf.almost_unitary(qf.H())
    assert qf.almost_unitary(qf.S())
    assert qf.almost_unitary(qf.T())
def test_canonical_decomp_sandwich():
    for _ in range(REPS):
        # Random CZ sandwich
        circ0 = qf.Circuit()
        circ0 += qf.random_gate([0])
        circ0 += qf.random_gate([1])
        circ0 += qf.CZ(0, 1)
        circ0 += qf.TY(0.4, 0)
        circ0 += qf.TY(0.25, 1)
        circ0 += qf.CZ(0, 1)
        circ0 += qf.random_gate([0])
        circ0 += qf.random_gate([1])

        gate0 = circ0.asgate()

        circ1 = qf.canonical_decomposition(gate0)
        gate1 = circ1.asgate()

        assert qf.gates_close(gate0, gate1)
        assert qf.almost_unitary(gate0)
Exemple #10
0
def test_tensor_properties(gatet: Type[qf.StdGate]) -> None:

    gate = _randomize_gate(gatet)

    assert qf.almost_unitary(gate)

    if gatet.cv_hermitian:
        assert gate is gate.H
        assert qf.almost_hermitian(gate)

    structure = gatet.cv_tensor_structure

    assert structure in (None, "identity", "diagonal", "permutation", "monomial")

    assert structure == _tensor_structure(gate.tensor)

    if gate.cv_interchangeable:
        qbs = list(gate.qubits)
        random.shuffle(qbs)
        perm_gate = gate.on(*qbs)
        assert qf.gates_close(gate, perm_gate)
Exemple #11
0
def test_not_unitary() -> None:
    assert not qf.almost_unitary(qf.P0())
    assert not qf.almost_unitary(qf.P1())
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())
def test_unitary_3qubit():
    assert qf.almost_unitary(qf.CCNOT())
    assert qf.almost_unitary(qf.CNOT())
    assert qf.almost_unitary(qf.CSWAP())
def test_unitary_2qubit():
    assert qf.almost_unitary(qf.CZ())
    assert qf.almost_unitary(qf.CNOT())
    assert qf.almost_unitary(qf.SWAP())
    assert qf.almost_unitary(qf.ISWAP())
Exemple #15
0
def test_random_gate():
    for n in range(1, 3):
        for _ in range(REPS):
            assert qf.almost_unitary(qf.random_gate(n))