Exemplo n.º 1
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))
Exemplo n.º 2
0
def test_projectors() -> None:
    ket = qf.zero_state(1)
    assert qf.P0(0).run(ket).norm() == 1.0

    ket = qf.H(0).run(ket)

    measure0 = qf.P0(0).run(ket)
    assert np.isclose(measure0.norm(), 0.5)

    measure1 = qf.P1(0).run(ket)
    assert np.isclose(measure1.norm(), 0.5)
Exemplo n.º 3
0
def test_projectors():
    ket = qf.zero_state(1)
    assert qf.asarray(qf.P0(0).run(ket).norm()) == 1.0

    ket = qf.H(0).run(ket)

    measure0 = qf.P0(0).run(ket)
    assert qf.asarray(measure0.norm()) * 2 == ALMOST_ONE

    measure1 = qf.P1(0).run(ket)
    assert qf.asarray(measure1.norm()) * 2 == ALMOST_ONE
Exemplo n.º 4
0
def test_measurement():
    rho = qf.zero_state(2).asdensity()
    chan = qf.H(0).aschannel()
    rho = chan.evolve(rho)
    rho = qf.Kraus([qf.P0(0), qf.P1(0)]).aschannel().evolve(rho)
    K = qf.Kraus([qf.P0(1), qf.P1(1)])
    chan = K.aschannel()

    rho = qf.Kraus([qf.P0(1), qf.P1(1)]).aschannel().evolve(rho)
    prob = qf.asarray(rho.probabilities())
    assert np.allclose(prob, [[0.5, 0], [0.5, 0]])
    assert prob[0, 0] * 2 == ALMOST_ONE
    assert prob[1, 0] * 2 == ALMOST_ONE
Exemplo n.º 5
0
def test_measurement() -> None:
    rho = qf.zero_state(2).asdensity()
    chan = qf.H(0).aschannel()
    rho = chan.evolve(rho)
    rho = qf.Kraus([qf.P0(0), qf.P1(0)]).aschannel().evolve(rho)
    K = qf.Kraus([qf.P0(1), qf.P1(1)])
    _ = K.aschannel()

    rho = qf.Kraus([qf.P0(1), qf.P1(1)]).aschannel().evolve(rho)
    prob = rho.probabilities()
    assert np.allclose(prob, [[0.5, 0], [0.5, 0]])
    assert np.isclose(prob[0, 0] * 2, 1.0)
    assert np.isclose(prob[1, 0] * 2, 1.0)
Exemplo n.º 6
0
def test_normalize():
    ket = qf.random_state(2)
    assert qf.asarray(ket.norm()) == ALMOST_ONE
    ket = qf.P0(0).run(ket)
    assert qf.asarray(ket.norm()) != ALMOST_ONE
    ket = ket.normalize()
    assert qf.asarray(ket.norm()) == ALMOST_ONE
Exemplo n.º 7
0
def test_normalize() -> None:
    ket = qf.random_state(2)
    assert np.isclose(ket.norm(), 1.0)

    ket = qf.P0(0).run(ket)
    assert ket.norm() < 1.0
    ket = ket.normalize()
    assert np.isclose(ket.norm(), 1.0)
Exemplo n.º 8
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)
Exemplo n.º 9
0
def swap_test(ket, q0: qf.Qubit, q1: qf.Qubit, q2: qf.Qubit) -> qf.QubitTensor:
    """
    Apply a Swap Test to measure fidelity between qubits q1 and q2.

    Qubit q0 is an ancilla, which should be zero state initially. The qubits
    cannot be initially entangled.
    """
    circ = qf.Circuit()
    circ += qf.H(q0)
    circ += qf.CSwap(q0, q1, q2)
    circ += qf.H(q0)
    circ += qf.P0(q0)  # Measure
    ket = circ.run(ket)

    fid = 2 * ket.norm() - 1.0  # return fidelity
    return fid
Exemplo n.º 10
0
def test_not_unitary() -> None:
    assert not qf.almost_unitary(qf.P0())
    assert not qf.almost_unitary(qf.P1())
Exemplo n.º 11
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()
Exemplo n.º 12
0
def test_kruas_qubits():
    rho = qf.Kraus([qf.P0(0), qf.P1(1)])
    assert rho.qubits == (0, 1)
    assert rho.qubit_nb == 2