def test_inverse_self():
    # These gates are their own inverse
    gate_names = ['I', 'X', 'Y', 'Z', 'CNOT', 'SWAP', 'CCNOT', 'CSWAP', 'CZ']

    for name in gate_names:
        gate = qf.STDGATES[name]()
        inv = gate.H
        assert type(gate) == type(inv)

        inv = qf.Gate(gate.tensor).H
        assert qf.gates_close(gate, inv)
Exemple #2
0
def test_transpose_map():
    # The transpose map is a superoperator that transposes a 1-qubit
    # density matrix. Not physical.
    # quant-ph/0202124

    ops = [
        qf.Gate(np.asarray([[1, 0], [0, 0]])),
        qf.Gate(np.asarray([[0, 0], [0, 1]])),
        qf.Gate(np.asarray([[0, 1], [1, 0]]) / np.sqrt(2)),
        qf.Gate(np.asarray([[0, 1], [-1, 0]]) / np.sqrt(2))
    ]

    kraus = qf.Kraus(ops, weights=(1, 1, 1, -1))
    rho0 = qf.random_density(1)
    rho1 = kraus.evolve(rho0)

    op0 = qf.asarray(rho0.asoperator())
    op1 = qf.asarray(rho1.asoperator())
    assert np.allclose(op0.T, op1)

    # The Choi matrix should be same as SWAP operator
    choi = kraus.aschannel().choi()
    choi = qf.asarray(choi)
    assert np.allclose(choi, qf.asarray(qf.SWAP(0, 2).asoperator()))
def test_kronecker_decomposition():
    for _ in range(REPS):
        left = qf.random_gate(1).vec.asarray()
        right = qf.random_gate(1).vec.asarray()
        both = np.kron(left, right)
        gate0 = qf.Gate(both, qubits=[0, 1])
        circ = qf.kronecker_decomposition(gate0)
        gate1 = circ.asgate()

        assert qf.gates_close(gate0, gate1)

    circ0 = qf.Circuit()
    circ0 += qf.Z(0)
    circ0 += qf.H(1)
    gate0 = circ.asgate()
    circ1 = qf.kronecker_decomposition(gate0)
    gate1 = circ1.asgate()

    assert qf.gates_close(gate0, gate1)
Exemple #4
0
def test_repr():
    g = qf.H()
    assert str(g) == 'H 0'

    g = qf.RX(3.12)
    assert str(g) == 'RX(3.12) 0'

    g = qf.identity_gate(2)
    assert str(g) == 'I 0 1'

    g = qf.random_gate(4)
    assert str(g) == 'RAND4 0 1 2 3'

    g = qf.H(0)
    assert str(g) == 'H 0'

    g = qf.CNOT(0, 1)
    assert str(g) == 'CNOT 0 1'

    g = qf.Gate(qf.CNOT().tensor)
    assert str(g).startswith('<quantumflow.ops.Gate')
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)