コード例 #1
0
def test_fubini_study_angle() -> None:

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

        ang = qf.fubini_study_angle(qf.I(0).tensor, qf.Rx(theta, 0).su().tensor)
        assert np.isclose(2 * ang / abs(theta), 1.0)

        ang = qf.fubini_study_angle(qf.I(0).tensor, qf.Ry(theta, 0).tensor)
        assert np.isclose(2 * ang / abs(theta), 1.0)

        ang = qf.fubini_study_angle(qf.I(0).tensor, qf.Rz(theta, 0).tensor)
        assert np.isclose(2 * ang / abs(theta), 1.0)

        ang = qf.fubini_study_angle(qf.Swap(0, 1).tensor, qf.PSwap(theta, 0, 1).tensor)

        assert np.isclose(2 * ang / abs(theta), 1.0)

        ang = qf.fubini_study_angle(qf.I(0).tensor, qf.PhaseShift(theta, 0).tensor)
        assert np.isclose(2 * ang / abs(theta), 1.0)

        assert qf.fubini_study_close(qf.Rz(theta, 0).tensor, qf.Rz(theta, 0).tensor)

    for n in range(1, 6):
        eye = qf.IdentityGate(list(range(n)))
        assert np.isclose(qf.fubini_study_angle(eye.tensor, eye.tensor), 0.0)

    with pytest.raises(ValueError):
        qf.fubini_study_angle(qf.RandomGate([1]).tensor, qf.RandomGate([0, 1]).tensor)
コード例 #2
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())
コード例 #3
0
def test_fubini_study_angle():

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

        ang = qf.asarray(qf.fubini_study_angle(qf.I().vec, qf.RX(theta).vec))
        assert 2 * ang / abs(theta) == ALMOST_ONE

        ang = qf.asarray(qf.fubini_study_angle(qf.I().vec, qf.RY(theta).vec))
        assert 2 * ang / abs(theta) == ALMOST_ONE

        ang = qf.asarray(qf.fubini_study_angle(qf.I().vec, qf.RZ(theta).vec))
        assert 2 * ang / abs(theta) == ALMOST_ONE

        ang = qf.asarray(
            qf.fubini_study_angle(qf.SWAP().vec,
                                  qf.PSWAP(theta).vec))
        assert 2 * ang / abs(theta) == ALMOST_ONE

        ang = qf.asarray(qf.fubini_study_angle(qf.I().vec,
                                               qf.PHASE(theta).vec))
        assert 2 * ang / abs(theta) == ALMOST_ONE

    for n in range(1, 6):
        eye = qf.identity_gate(n)
        assert qf.asarray(qf.fubini_study_angle(eye.vec, eye.vec)) \
            == ALMOST_ZERO

    with pytest.raises(ValueError):
        qf.fubini_study_angle(qf.random_gate(1).vec, qf.random_gate(2).vec)
コード例 #4
0
def test_gates_close():
    assert not qf.gates_close(qf.I(), qf.identity_gate(2))
    assert qf.gates_close(qf.I(), qf.I())
    assert qf.gates_close(qf.P0(), qf.P0())
    assert not qf.gates_close(qf.P0(), qf.P1())

    assert qf.gates_close(qf.CNOT(0, 1), qf.CNOT(0, 1))
    assert not qf.gates_close(qf.CNOT(1, 0), qf.CNOT(0, 1))
コード例 #5
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())
コード例 #6
0
def test_diamond_norm() -> None:
    # Test cases borrowed from qutip,
    # https://github.com/qutip/qutip/blob/master/qutip/tests/test_metrics.py
    # which were in turn  generated using QuantumUtils for MATLAB
    # (https://goo.gl/oWXhO9)

    RTOL = 0.01
    chan0 = qf.I(0).aschannel()
    chan1 = qf.X(0).aschannel()
    dn = diamond_norm(chan0, chan1)
    assert np.isclose(2.0, dn, rtol=RTOL)

    turns_dnorm = [
        [1.000000e-03, 3.141591e-03],
        [3.100000e-03, 9.738899e-03],
        [1.000000e-02, 3.141463e-02],
        [3.100000e-02, 9.735089e-02],
        [1.000000e-01, 3.128689e-01],
        [3.100000e-01, 9.358596e-01],
    ]

    for turns, target in turns_dnorm:
        chan0 = qf.XPow(0.0, 0).aschannel()
        chan1 = qf.XPow(turns, 0).aschannel()

        dn = diamond_norm(chan0, chan1)
        assert np.isclose(target, dn, rtol=RTOL)

    hadamard_mixtures = [
        [1.000000e-03, 2.000000e-03],
        [3.100000e-03, 6.200000e-03],
        [1.000000e-02, 2.000000e-02],
        [3.100000e-02, 6.200000e-02],
        [1.000000e-01, 2.000000e-01],
        [3.100000e-01, 6.200000e-01],
    ]

    for p, target in hadamard_mixtures:
        tensor = qf.I(0).aschannel().tensor * (
            1 - p) + qf.H(0).aschannel().tensor * p
        chan0 = qf.Channel(tensor, [0])

        chan1 = qf.I(0).aschannel()

        dn = diamond_norm(chan0, chan1)
        assert np.isclose(dn, target, rtol=RTOL)

    chan0 = qf.YPow(0.5, 0).aschannel()
    chan1 = qf.I(0).aschannel()
    dn = diamond_norm(chan0, chan1)
    assert np.isclose(dn, np.sqrt(2), rtol=RTOL)

    chan0 = qf.CNot(0, 1).aschannel()
    chan1 = qf.CNot(1, 0).aschannel()
    diamond_norm(chan0, chan1)
コード例 #7
0
def test_diamond_norm():
    # Test cases borrowed from qutip,
    # https://github.com/qutip/qutip/blob/master/qutip/tests/test_metrics.py
    # which were in turn  generated using QuantumUtils for MATLAB
    # (https://goo.gl/oWXhO9)
    RTOL = 0.01
    chan0 = qf.I(0).aschannel()
    chan1 = qf.X(0).aschannel()
    dn = qf.diamond_norm(chan0, chan1)
    assert np.isclose(2.0, dn, rtol=RTOL)

    turns_dnorm = [[1.000000e-03, 3.141591e-03],
                   [3.100000e-03, 9.738899e-03],
                   [1.000000e-02, 3.141463e-02],
                   [3.100000e-02, 9.735089e-02],
                   [1.000000e-01, 3.128689e-01],
                   [3.100000e-01, 9.358596e-01]]

    for turns, target in turns_dnorm:
        chan0 = qf.TX(0).aschannel()
        chan1 = qf.TX(turns).aschannel()

        dn = qf.diamond_norm(chan0, chan1)
        assert np.isclose(target, dn, rtol=RTOL)

    hadamard_mixtures = [[1.000000e-03, 2.000000e-03],
                         [3.100000e-03, 6.200000e-03],
                         [1.000000e-02, 2.000000e-02],
                         [3.100000e-02, 6.200000e-02],
                         [1.000000e-01, 2.000000e-01],
                         [3.100000e-01, 6.200000e-01]]

    for p, target in hadamard_mixtures:
        # FIXME: implement __rmul__ for channels
        chan0 = qf.I(0).aschannel() * (1 - p) + qf.H(0).aschannel() * p
        chan1 = qf.I(0).aschannel()

        dn = qf.diamond_norm(chan0, chan1)
        assert np.isclose(dn, target, rtol=RTOL)

    chan0 = qf.TY(0.5, 0).aschannel()
    chan1 = qf.I(0).aschannel()
    dn = qf.diamond_norm(chan0, chan1)
    assert np.isclose(dn, np.sqrt(2), rtol=RTOL)

    chan0 = qf.CNOT(0, 1).aschannel()
    chan1 = qf.CNOT(1, 0).aschannel()
    qf.diamond_norm(chan0, chan1)
コード例 #8
0
def test_bloch_decomposition():
    theta = 1.23

    gate0 = qf.RN(theta, 1, 0, 0)
    gate1 = qf.bloch_decomposition(gate0).elements[0]
    assert qf.gates_close(gate0, gate1)

    gate0 = qf.RN(theta, 0, 1, 0)
    gate1 = qf.bloch_decomposition(gate0).elements[0]
    assert qf.gates_close(gate0, gate1)

    gate0 = qf.RN(theta, 0, 0, 1)
    gate1 = qf.bloch_decomposition(gate0).elements[0]
    assert qf.gates_close(gate0, gate1)

    gate0 = qf.RN(pi, np.sqrt(2), 0, np.sqrt(2))
    gate1 = qf.bloch_decomposition(gate0).elements[0]
    assert qf.gates_close(gate0, gate1)

    for _ in range(REPS):
        gate0 = qf.random_gate(qubits=[0])
        gate1 = qf.bloch_decomposition(gate0).elements[0]
        assert qf.gates_close(gate0, gate1)

    gate0 = qf.I(0)
    gate1 = qf.bloch_decomposition(gate0).elements[0]
    assert qf.gates_close(gate0, gate1)
コード例 #9
0
def test_decomp_stdgates():
    gate0 = qf.I(0, 1)
    gate1 = qf.canonical_decomposition(gate0).asgate()
    assert qf.gates_close(gate0, gate1)

    gate0 = qf.CNOT(0, 1)
    gate1 = qf.canonical_decomposition(gate0).asgate()
    assert qf.gates_close(gate0, gate1)

    gate0 = qf.SWAP(0, 1)
    gate1 = qf.canonical_decomposition(gate0).asgate()
    assert qf.gates_close(gate0, gate1)

    gate0 = qf.ISWAP(0, 1)
    gate1 = qf.canonical_decomposition(gate0).asgate()
    assert qf.gates_close(gate0, gate1)

    gate0 = qf.CNOT(0, 1)**0.5
    gate1 = qf.canonical_decomposition(gate0).asgate()
    assert qf.gates_close(gate0, gate1)

    gate0 = qf.SWAP(0, 1)**0.5
    gate1 = qf.canonical_decomposition(gate0).asgate()
    assert qf.gates_close(gate0, gate1)

    gate0 = qf.ISWAP(0, 1)**0.5
    gate1 = qf.canonical_decomposition(gate0).asgate()
    assert qf.gates_close(gate0, gate1)
コード例 #10
0
def test_bloch_decomposition() -> None:
    theta = 1.23

    gate0 = qf.Rn(theta, 1, 0, 0, 9)
    gate1 = qf.bloch_decomposition(gate0)[0]
    assert isinstance(gate1, qf.Gate)
    assert qf.gates_close(gate0, gate1)

    gate0 = qf.Rn(theta, 0, 1, 0, 9)
    gate1 = qf.bloch_decomposition(gate0)[0]
    assert isinstance(gate1, qf.Gate)
    assert qf.gates_close(gate0, gate1)

    gate0 = qf.Rn(theta, 0, 0, 1, 9)
    gate1 = qf.bloch_decomposition(gate0)[0]
    assert isinstance(gate1, qf.Gate)
    assert qf.gates_close(gate0, gate1)

    gate0 = qf.Rn(pi, np.sqrt(2), 0, np.sqrt(2), 9)
    gate1 = qf.bloch_decomposition(gate0)[0]
    assert isinstance(gate1, qf.Gate)
    assert qf.gates_close(gate0, gate1)

    for _ in range(REPS):
        gate3 = qf.RandomGate(qubits=[0])
        gate4 = qf.bloch_decomposition(gate3)[0]
        assert isinstance(gate4, qf.Gate)
        assert qf.gates_close(gate3, gate4)

    gate5 = qf.I(0)
    gate6 = qf.bloch_decomposition(gate5)[0]
    assert isinstance(gate6, qf.Gate)
    assert qf.gates_close(gate5, gate6)
コード例 #11
0
def test_PauliGate() -> None:
    pauli0 = 0.5 * np.pi * qf.sX(0) * qf.sX(1)

    alpha = 0.4
    circ = qf.PauliGate(pauli0, alpha)
    coords = qf.canonical_coords(circ.asgate())
    assert np.isclose(coords[0], 0.4)

    pauli1 = np.pi * qf.sX(0) * qf.sX(1) * qf.sY(2) * qf.sZ(3)
    _ = qf.PauliGate(pauli1, alpha)

    top2 = nx.star_graph(4)
    pauli2 = 0.5 * np.pi * qf.sX(1) * qf.sY(2) * qf.sZ(3)
    _ = qf.PauliGate(pauli2, alpha).decompose(top2)

    alpha = 0.2
    top3 = nx.star_graph(4)
    pauli3 = 0.5 * np.pi * qf.sX(1) * qf.sX(2)
    circ3 = qf.Circuit(qf.PauliGate(pauli3, alpha).decompose(top3))

    assert qf.circuits_close(circ3, qf.Circuit([qf.I(0), qf.XX(alpha, 1, 2)]))

    qf.PauliGate(qf.sI(0), alpha).decompose(top2)

    with pytest.raises(ValueError):
        pauli4 = 0.5j * np.pi * qf.sX(1) * qf.sX(2)
        _ = qf.Circuit(qf.PauliGate(pauli4, alpha).decompose(top3))

    top4 = nx.DiGraph()
    nx.add_path(top4, [3, 2, 1, 0])
    _ = qf.Circuit(qf.PauliGate(pauli3, alpha).decompose(top4))
コード例 #12
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))
コード例 #13
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())
コード例 #14
0
def test_cirq_simulator() -> None:
    q0, q1, q2 = "q0", "q1", "q2"

    circ0 = qf.Circuit()
    circ0 += qf.I(q0)
    circ0 += qf.I(q1)
    circ0 += qf.I(q2)
    circ0 += qf.X(q1)
    circ0 += qf.Y(q2)

    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.XPow(0.6, q0)
    circ0 += qf.YPow(0.6, q1)
    circ0 += qf.ZPow(0.6, q2)

    circ0 += qf.XX(0.2, q0, q1)
    circ0 += qf.YY(0.3, q1, q2)
    circ0 += qf.ZZ(0.4, q2, q0)

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

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

    ket0 = qf.random_state([q0, q1, q2])
    ket1 = circ0.run(ket0)
    sim = CirqSimulator(circ0)
    ket2 = sim.run(ket0)

    assert ket1.qubits == ket2.qubits

    print(qf.state_angle(ket1, ket2))
    assert qf.states_close(ket1, ket2)

    assert qf.states_close(circ0.run(), sim.run())
コード例 #15
0
def test_gates_to_latex():
    circ = qf.Circuit()

    circ += qf.I(7)
    circ += qf.X(0)
    circ += qf.Y(1)
    circ += qf.Z(2)
    circ += qf.H(3)
    circ += qf.S(4)
    circ += qf.T(5)
    circ += qf.S_H(6)
    circ += qf.T_H(7)

    circ += qf.RX(-0.5*pi, 0)
    circ += qf.RY(0.5*pi, 1)
    circ += qf.RZ((1/3)*pi, 1)
    circ += qf.RY(0.222, 1)

    circ += qf.TX(0.5, 0)
    circ += qf.TY(0.5, 1)
    circ += qf.TZ(0.4, 1)

    circ += qf.TZ(0.47276, 1)
    # Gate with cunning hack
    gate = qf.RZ(0.4, 1)
    gate.params['theta'] = qf.Parameter('\\theta')
    circ += gate

    circ += qf.CNOT(1, 2)
    circ += qf.CNOT(2, 1)
    circ += qf.CZ(1, 3)
    circ += qf.SWAP(1, 5)
    circ += qf.ISWAP(4, 2)

    # circ += qf.Barrier(0, 1, 2, 3, 4, 5, 6)  # Not yet supported

    circ += qf.CCNOT(1, 2, 3)
    circ += qf.CSWAP(4, 5, 6)

    circ += qf.P0(0)
    circ += qf.P1(1)

    circ += qf.Reset(2)
    circ += qf.Reset(4, 5, 6)

    circ += qf.H(4)
    # circ += qf.Reset()    # FIXME. Should fail with clear error message

    circ += qf.XX(0.25, 1, 3)
    circ += qf.YY(0.75, 1, 3)
    circ += qf.ZZ(1/3, 3, 1)

    circ += qf.Measure(0)

    latex = qf.circuit_to_latex(circ)

    print(latex)
コード例 #16
0
def test_hadamard():
    gate = qf.I()
    gate = qf.RZ(pi / 2, 0) @ gate
    gate = qf.RX(pi / 2, 0) @ gate
    gate = qf.RZ(pi / 2, 0) @ gate

    res = qf.asarray(qf.inner_product(gate.vec, qf.H().vec))

    assert abs(res) / 2 == ALMOST_ONE
コード例 #17
0
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)
コード例 #18
0
def test_inverse_tgates_1qubit():
    gates = qf.TX, qf.TY, qf.TZ

    for gate in gates:
        for _ in range(REPS):
            t = random.uniform(-2, +2)
            g = gate(t)
            inv = g.H
            assert qf.gates_close(qf.I(), g @ inv)
            assert type(g) == type(inv)
コード例 #19
0
def test_inverse_parametric_1qubit():
    gates = [qf.PHASE, qf.RX, qf.RY, qf.RZ]

    for gate in gates:
        for _ in range(REPS):
            theta = random.uniform(-4 * pi, +4 * pi)
            g = gate(theta)
            inv = g.H
            assert qf.gates_close(qf.I(), g @ inv)
            assert type(g) == type(inv)
コード例 #20
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))
コード例 #21
0
def test_outer_product():
    with pytest.raises(ValueError):
        ket = qf.zero_state(1)
        gate = qf.I(2)
        qf.outer_product(ket.vec, gate.vec)

    with pytest.raises(ValueError):
        ket0 = qf.zero_state([0, 1, 2])
        ket1 = qf.zero_state([2, 3, 4])
        qf.outer_product(ket0.vec, ket1.vec)
コード例 #22
0
def _cli():

    gates = [
        qf.I(0),
        qf.X(0),
        qf.Y(0),
        qf.Z(0),
        qf.S(0),
        qf.T(0),
        qf.H(0),
        qf.XPow(0.2, 0),
        qf.YPow(0.2, 0),
        qf.ZPow(0.2, 0),
        qf.CNot(0, 1),
        qf.CZ(0, 1),
        qf.Swap(0, 1),
        qf.ISwap(0, 1),
        qf.CCNot(0, 1, 2),
        qf.CCZ(0, 1, 2),
        qf.CSwap(0, 1, 2),
    ]

    print()
    print("Gate     QF GOPS         Cirq GOPS")

    # for n in range(4):
    #     circ = benchmark_circuit(QUBITS, GATES, qf.RandomGate([0,1]))
    #     t = timeit.timeit(lambda: circ.run(), number=REPS,
    #                       timer=time.process_time)
    #     gops = int((GATES*REPS)/t)
    #     gops = int((gops * 100) + 0.5) / 100.0
    #     print(f"gate qubits: {n}  gops:{gops}")

    for gate in gates:
        circ = benchmark_circuit(QUBITS, GATES, gate)
        t = timeit.timeit(lambda: circ.run(),
                          number=REPS,
                          timer=time.process_time)

        cq = qf.xcirq.CirqSimulator(circ)
        t2 = timeit.timeit(lambda: cq.run(),
                           number=REPS,
                           timer=time.process_time)

        gops = int((GATES * REPS) / t)
        gops = int((gops * 100) + 0.5) / 100.0

        gops2 = int((GATES * REPS) / t2)
        gops2 = int((gops2 * 100) + 0.5) / 100.0

        if gops / gops2 > 0.8:
            print(gate.name, "\t", gops, "\t", gops2)
        else:
            print(gate.name, "\t", gops, "\t", gops2, "\t☹️")
コード例 #23
0
def test_circuit_to_circ() -> None:
    q0, q1, q2 = "q0", "q1", "q2"

    circ0 = qf.Circuit()
    circ0 += qf.I(q0)
    circ0 += qf.X(q1)
    circ0 += qf.Y(q2)

    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.XPow(0.6, q0)
    circ0 += qf.YPow(0.6, q1)
    circ0 += qf.ZPow(0.6, q2)

    circ0 += qf.XX(0.2, q0, q1)
    circ0 += qf.YY(0.3, q1, q2)
    circ0 += qf.ZZ(0.4, q2, q0)

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

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

    circ0 += qf.FSim(1, 2, q0, q1)

    diag0 = qf.circuit_to_diagram(circ0)
    # print()
    # print(diag0)

    cqc = circuit_to_cirq(circ0)
    # print(cqc)
    circ1 = cirq_to_circuit(cqc)

    diag1 = qf.circuit_to_diagram(circ1)
    # print()
    # print(diag1)

    assert diag0 == diag1
コード例 #24
0
def test_qsim_simulator() -> None:
    q0, q1, q2 = "q0", "q1", "q2"

    circ0 = qf.Circuit()

    circ0 += qf.I(q0)

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

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

    # Waiting for bugfix in qsim
    circ0 += qf.Z(q1)**0.2
    circ0 += qf.X(q1)**0.2
    circ0 += qf.XPow(0.2, q0)
    circ0 += qf.YPow(0.2, q1)
    circ0 += qf.ZPow(0.5, q2)

    circ0 += qf.CZ(q0, q1)
    circ0 += qf.CNot(q0, q1)
    # circ0 += qf.SWAP(q0, q1)   # No SWAP!
    #  circ0 += qf.ISWAP(q0, q1) # Waiting for bugfix in qsim
    circ0 += qf.FSim(0.1, 0.2, q0, q1)

    # No 3-qubit gates

    # Initial state not yet supported in qsim
    # ket0 = qf.random_state([q0, q1, q2])
    ket1 = circ0.run()
    sim = QSimSimulator(circ0)
    ket2 = sim.run()

    assert ket1.qubits == ket2.qubits

    print("QF", ket1)
    print("QS", ket2)

    assert qf.states_close(ket1, ket2)
    assert qf.states_close(circ0.run(), sim.run())
コード例 #25
0
def test_partial_trace():
    data = [1] * (2**16)

    r8 = qf.QubitVector(data, range(2))
    r4 = qf.QubitVector(data, range(4))
    r2 = qf.QubitVector(data, range(8))

    tr2 = r2.partial_trace([1])
    assert tr2.qubits == (0, 2, 3, 4, 5, 6, 7)
    assert tr2.rank == 2

    tr2 = r2.partial_trace([2, 3])
    assert tr2.qubits == (0, 1, 4, 5, 6, 7)
    assert tr2.rank == 2

    tr4 = r4.partial_trace([0])
    assert tr4.qubits == (1, 2, 3)
    assert tr4.rank == 4

    tr8 = r8.partial_trace([1])
    assert tr8.qubits == (0, )
    assert tr8.rank == 8

    with pytest.raises(ValueError):
        r2.partial_trace(range(8))

    chan012 = qf.identity_gate(3).aschannel()
    assert np.isclose(qf.asarray(chan012.trace()), 64)  # 2**(2**3)

    chan02 = chan012.partial_trace([1])
    assert np.isclose(qf.asarray(chan02.trace()), 32)  # TODO: Checkme
    chan2 = chan012.partial_trace([0, 1])

    assert np.isclose(qf.asarray(chan2.trace()), 16)  # TODO: checkme

    # partial traced channels should be identities still, upto normalization
    assert qf.channels_close(chan2, qf.I(2).aschannel())
    # TODO: Channel.normalize()

    with pytest.raises(ValueError):
        qf.zero_state(4).vec.partial_trace([1, 2])
コード例 #26
0
def test_I():
    assert np.allclose(qf.I().vec.asarray(), np.eye(2))
    assert np.allclose(qf.I(0, 1).vec.asarray().reshape((4, 4)), np.eye(4))
コード例 #27
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)
コード例 #28
0
def test_parametric_Y():
    pseudo_hadamard = qf.TY(1.5)
    inv_pseudo_hadamard = qf.TY(0.5)
    gate = pseudo_hadamard @ inv_pseudo_hadamard
    assert qf.gates_close(gate, qf.I())
コード例 #29
0
def test_channel_trace():
    chan = qf.I(0).aschannel()
    assert np.isclose(qf.asarray(chan.trace()), 4)
コード例 #30
0
def test_visualize_circuit() -> None:
    circ = qf.Circuit()

    circ += qf.I(7)
    circ += qf.X(0)
    circ += qf.Y(1)
    circ += qf.Z(2)
    circ += qf.H(3)
    circ += qf.S(4)
    circ += qf.T(5)
    circ += qf.S_H(6)
    circ += qf.T_H(7)

    circ += qf.Rx(-0.5 * pi, 0)
    circ += qf.Ry(0.5 * pi, 4)
    circ += qf.Rz((1 / 3) * pi, 5)
    circ += qf.Ry(0.222, 6)

    circ += qf.XPow(0.5, 0)
    circ += qf.YPow(0.5, 2)
    circ += qf.ZPow(0.4, 2)
    circ += qf.HPow(0.5, 3)
    circ += qf.ZPow(0.47276, 1)

    # Gate with symbolic parameter
    #  gate = qf.Rz(Symbol('\\theta'), 1)
    # circ += gate

    circ += qf.CNot(1, 2)
    circ += qf.CNot(2, 1)
    # circ += qf.IDEN(*range(8))
    circ += qf.ISwap(4, 2)
    circ += qf.ISwap(6, 5)
    circ += qf.CZ(1, 3)
    circ += qf.Swap(1, 5)

    # circ += qf.Barrier(0, 1, 2, 3, 4, 5, 6)  # Not yet supported in latex

    circ += qf.CCNot(1, 2, 3)
    circ += qf.CSwap(4, 5, 6)

    circ += qf.P0(0)
    circ += qf.P1(1)

    circ += qf.Reset(2)
    circ += qf.Reset(4, 5, 6)

    circ += qf.H(4)

    circ += qf.XX(0.25, 1, 4)
    circ += qf.XX(0.25, 1, 2)
    circ += qf.YY(0.75, 1, 3)
    circ += qf.ZZ(1 / 3, 3, 1)

    circ += qf.CPhase(0, 0, 1)
    circ += qf.CPhase(pi * 1 / 2, 0, 4)

    circ += qf.Can(1 / 3, 1 / 2, 1 / 2, 0, 1)
    circ += qf.Can(1 / 3, 1 / 2, 1 / 2, 2, 4)
    circ += qf.Can(1 / 3, 1 / 2, 1 / 2, 6, 5)

    # circ += qf.Measure(0)
    # circ += qf.Measure(1, 1)

    circ += qf.PSwap(pi / 2, 6, 7)

    circ += qf.Ph(1 / 4, 7)

    circ += qf.CH(1, 6)

    circ += qf.visualization.NoWire([0, 1, 2])
    # circ += qf.visualization.NoWire(4, 1, 2)

    if os.environ.get("QF_VIZTEST"):
        print()
        print(qf.circuit_to_diagram(circ))

    qf.circuit_to_diagram(circ)

    qf.circuit_to_latex(circ)
    qf.circuit_to_latex(circ, package="qcircuit")
    qf.circuit_to_latex(circ, package="quantikz")

    qf.circuit_to_diagram(circ)
    qf.circuit_to_diagram(circ, use_unicode=False)

    latex = qf.circuit_to_latex(circ, package="qcircuit")
    print(latex)
    if os.environ.get("QF_VIZTEST"):
        qf.latex_to_image(latex).show()

    latex = qf.circuit_to_latex(circ, package="quantikz")
    print(latex)

    if os.environ.get("QF_VIZTEST"):
        qf.latex_to_image(latex).show()