コード例 #1
0
def test_can_to_cnot() -> None:
    gate = qf.Can(0.3, 0.23, 0.22, 0, 1)
    circ = qf.Circuit(qf.translate_can_to_cnot(gate))  # type: ignore
    assert qf.gates_close(gate, circ.asgate())

    gate = qf.Can(0.3, 0.23, 0.0, 0, 1)
    circ = qf.Circuit(qf.translate_can_to_cnot(gate))  # type: ignore
    print(qf.canonical_decomposition(circ.asgate()))
    assert qf.gates_close(gate, circ.asgate())
コード例 #2
0
def test_decomp_sqrtswap_sandwich() -> None:
    circ0 = qf.Circuit()
    circ0 += qf.Can(1 / 4, 1 / 4, 1 / 4, 0, 1)
    circ0 += qf.RandomGate([0])
    circ0 += qf.RandomGate([1])
    circ0 += qf.Can(1 / 4, 1 / 4, 1 / 4, 0, 1)

    gate0 = circ0.asgate()
    circ1 = qf.canonical_decomposition(gate0)
    gate1 = circ1.asgate()
    assert qf.gates_close(gate0, gate1)
コード例 #3
0
def test_translate_to_qutip() -> None:
    circ0 = qf.Circuit()
    circ0 += qf.Can(0.1, 0.2, 0.3, 0, 1)
    qbc = xqutip.circuit_to_qutip(circ0, translate=True)
    U = gate_sequence_product(qbc.propagators())
    gate0 = qf.Unitary(U.full(), qubits=[0, 1])
    assert qf.gates_close(gate0, circ0.asgate())

    with pytest.raises(ValueError):
        xqutip.circuit_to_qutip(circ0, translate=False)

    circ1 = qf.Circuit()
    circ1 += qf.Can(0.1, 0.2, 0.3, "a", "b")
    with pytest.raises(ValueError):
        xqutip.circuit_to_qutip(circ1, translate=True)
コード例 #4
0
def sandwich_decompositions(coords0, coords1, samples=SAMPLES):
    """Create composite gates, decompose, and return a list
    of canonical coordinates"""
    decomps = []
    for _ in range(samples):
        circ = qf.Circuit()
        circ += qf.Can(*coords0, 0, 1)
        circ += qf.RandomGate([0])
        circ += qf.RandomGate([1])
        circ += qf.Can(*coords1, 0, 1)
        gate = circ.asgate()

        coords = qf.canonical_coords(gate)
        decomps.append(coords)

    return decomps
コード例 #5
0
def test_canonical_decomposition() -> None:
    for tt1 in range(0, 6):
        for tt2 in range(tt1):
            for tt3 in range(tt2):
                t1, t2, t3 = tt1 / 12, tt2 / 12, tt3 / 12
                if t3 == 0 and t1 > 0.5:
                    continue
                coords = np.asarray((t1, t2, t3))

                circ0 = qf.Circuit()
                circ0 += qf.RandomGate([0])
                circ0 += qf.RandomGate([1])
                circ0 += qf.Can(t1, t2, t3, 0, 1)
                circ0 += qf.RandomGate([0])
                circ0 += qf.RandomGate([1])
                gate0 = circ0.asgate()

                circ1 = qf.canonical_decomposition(gate0)
                assert qf.gates_close(gate0, circ1.asgate())

                canon = circ1[1]
                new_coords = np.asarray(
                    [canon.param(n) for n in ["tx", "ty", "tz"]])
                assert np.allclose(coords, np.asarray(new_coords))

                coords2 = qf.canonical_coords(gate0)
                assert np.allclose(coords, np.asarray(coords2))
コード例 #6
0
def test_convert_can_to_weyl() -> None:
    for r in range(100):
        tx = np.random.uniform(-10, +10)
        ty = np.random.uniform(-10, +10)
        tz = np.random.uniform(-10, +10)

        gate0 = qf.Can(tx, ty, tz, 1, 2)
        circ = qf.convert_can_to_weyl(gate0, euler="XYX")

        assert qf.gates_close(gate0, circ.asgate())
コード例 #7
0
def test_qsim_translate() -> None:
    q0, q1, q2 = "q0", "q1", "q2"

    circ0 = qf.Circuit()
    circ0 += qf.H(q0)
    circ0 += qf.X(q1)
    circ0 += qf.S_H(q0)
    circ0 += qf.XX(0.2, q0, q1)
    circ0 += qf.Can(0.2, 0.1, 0.4, q0, q2)
    circ0 += qf.Swap(q0, q1)

    ket1 = circ0.run()
    sim = QSimSimulator(circ0, translate=True)
    # print(sim._circuit)
    ket2 = sim.run()

    assert qf.states_close(ket1, ket2)
コード例 #8
0
def test_specialize_2q() -> None:
    q0, q1 = 3, 5
    assert isinstance(qf.Can(0.0, 0.0, 0.0, q0, q1).specialize(), qf.I)
    assert isinstance(qf.Can(0.213, 0.0, 0.0, q0, q1).specialize(), qf.XX)
    assert isinstance(qf.Can(0.0, 0.213, 0.0, q0, q1).specialize(), qf.YY)
    assert isinstance(qf.Can(0.0, 0.0, 0.213, q0, q1).specialize(), qf.ZZ)
    assert isinstance(
        qf.Can(0.213, 0.213, 0.213, q0, q1).specialize(), qf.Exch)
    assert isinstance(qf.Can(0.5, 0.32, 0.213, q0, q1).specialize(), qf.Can)

    assert isinstance(qf.CNotPow(0.2, q0, q1).specialize(), qf.CNotPow)
    assert isinstance(qf.CNotPow(0.0, q0, q1).specialize(), qf.I)
    assert isinstance(qf.CNotPow(1.0, q0, q1).specialize(), qf.CNot)

    assert isinstance(qf.XX(0.0, q0, q1).specialize(), qf.I)
    assert isinstance(qf.YY(0.0, q0, q1).specialize(), qf.I)
    assert isinstance(qf.ZZ(0.0, q0, q1).specialize(), qf.I)
    assert isinstance(qf.Exch(0.0, q0, q1).specialize(), qf.I)
コード例 #9
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()
コード例 #10
0
def test_circuit_to_circ_translate() -> None:
    circ0 = qf.Circuit([qf.Can(0.2, 0.3, 0.1, 0, 1)])
    cqc = circuit_to_cirq(circ0, translate=True)
    circ2 = cirq_to_circuit(cqc)
    assert qf.circuits_close(circ0, circ2)
コード例 #11
0
def test_circuit_to_circ_exception() -> None:
    circ0 = qf.Circuit([qf.Can(0.2, 0.3, 0.1, 0, 1)])
    with pytest.raises(NotImplementedError):
        circuit_to_cirq(circ0)
コード例 #12
0
def test_fail() -> None:
    with pytest.raises(ValueError):
        circ = qf.Circuit([qf.Can(0.1, 0.2, 0.3, 0, 1)])
        quirk_url(circuit_to_quirk(circ))