def test_gate3_to_diagrams() -> None:
    circ = qf.Circuit()
    circ += qf.CCNot(0, 1, 2)
    circ += qf.CCNot(0, 2, 1)
    circ += qf.CSwap(0, 1, 2)
    circ += qf.CSwap(1, 0, 2)
    circ += qf.CCZ(0, 1, 2)
    circ += qf.CCiX(0, 1, 2)
    circ += qf.CCNot(0, 1, 2)**0.25
    circ += qf.Deutsch(0.25, 0, 1, 2)

    circ += qf.CV(1, 0)
    circ += qf.CV_H(1, 0)

    print()

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

    latex = qf.circuit_to_latex(circ)

    if os.environ.get("QF_VIZTEST"):
        qf.latex_to_image(latex).show()
Exemplo n.º 2
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☹️")
Exemplo n.º 3
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
Exemplo n.º 4
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())
Exemplo n.º 5
0
def test_circuit_to_quirk() -> None:
    # 2-qubit gates

    quirk = "https://algassert.com/quirk#circuit={%22cols%22:[[1,%22X%22,%22%E2%80%A2%22],[%22%E2%80%A2%22,1,%22Z%22],[1,%22%E2%80%A2%22,%22Y%22],[%22Swap%22,1,%22Swap%22]]}"  # noqa: E501
    circ = qf.Circuit([qf.CNot(2, 1), qf.CZ(0, 2), qf.CY(1, 2), qf.Swap(0, 2)])
    print()
    print(urllib.parse.unquote(quirk))
    print(quirk_url(circuit_to_quirk(circ)))
    assert urllib.parse.unquote(quirk) == quirk_url(circuit_to_quirk(circ))

    # 3-qubit gates
    quirk = "https://algassert.com/quirk#circuit={%22cols%22:[[%22%E2%80%A2%22,%22%E2%80%A2%22,%22X%22],[%22%E2%80%A2%22,%22%E2%80%A2%22,%22Z%22],[%22%E2%80%A2%22,%22Swap%22,%22Swap%22]]}"  # noqa: E501
    circ = qf.Circuit([qf.CCNot(0, 1, 2), qf.CCZ(0, 1, 2), qf.CSwap(0, 1, 2)])
    print()
    print(urllib.parse.unquote(quirk))
    print(quirk_url(circuit_to_quirk(circ)))
    assert urllib.parse.unquote(quirk) == quirk_url(circuit_to_quirk(circ))

    test0 = "https://algassert.com/quirk#circuit={%22cols%22:[[%22Z%22,%22Y%22,%22X%22,%22H%22]]}"  # noqa: E501
    test0 = urllib.parse.unquote(test0)
    circ = qf.Circuit([qf.Z(0), qf.Y(1), qf.X(2), qf.H(3)])
    print(test0)
    print(quirk_url(circuit_to_quirk(circ)))
    assert test0 == quirk_url(circuit_to_quirk(circ))

    test_halfturns = "https://algassert.com/quirk#circuit={%22cols%22:[[%22X^%C2%BD%22,%22Y^%C2%BD%22,%22Z^%C2%BD%22],[%22X^-%C2%BD%22,%22Y^-%C2%BD%22,%22Z^-%C2%BD%22]]}"  # noqa: E501
    test_halfturns = urllib.parse.unquote(test_halfturns)
    circ = qf.Circuit(
        [qf.V(0),
         qf.SqrtY(1),
         qf.S(2),
         qf.V(0).H,
         qf.SqrtY(1).H,
         qf.S(2).H])
    print(test_halfturns)
    print(quirk_url(circuit_to_quirk(circ)))
    assert test_halfturns == quirk_url(circuit_to_quirk(circ))

    quarter_turns = "https://algassert.com/quirk#circuit={%22cols%22:[[%22Z^%C2%BC%22],[%22Z^-%C2%BC%22]]}"  # noqa: E501
    s = urllib.parse.unquote(quarter_turns)
    circ = qf.Circuit([qf.T(0), qf.T(0).H])
    assert s == quirk_url(circuit_to_quirk(circ))

    # GHZ circuit
    quirk = "https://algassert.com/quirk#circuit={%22cols%22:[[%22H%22],[%22%E2%80%A2%22,%22X%22],[1,%22%E2%80%A2%22,%22X%22]]}"  # noqa: E501
    circ = qf.Circuit([qf.H(0), qf.CNot(0, 1), qf.CNot(1, 2)])
    print(urllib.parse.unquote(quirk))
    print(quirk_url(circuit_to_quirk(circ)))
    assert urllib.parse.unquote(quirk) == quirk_url(circuit_to_quirk(circ))

    test_formulaic = "https://algassert.com/quirk#circuit={%22cols%22:[[{%22id%22:%22X^ft%22,%22arg%22:%220.1%22},{%22id%22:%22Y^ft%22,%22arg%22:%220.2%22},{%22id%22:%22Z^ft%22,%22arg%22:%220.3%22}],[{%22id%22:%22Rxft%22,%22arg%22:%220.4%22},{%22id%22:%22Ryft%22,%22arg%22:%220.5%22},{%22id%22:%22Rzft%22,%22arg%22:%220.6%22}]]}"  # noqa: E501
    s = urllib.parse.unquote(test_formulaic)
    circ = qf.Circuit([
        qf.XPow(0.1, 0),
        qf.YPow(0.2, 1),
        qf.ZPow(0.3, 2),
        qf.Rx(0.4, 0),
        qf.Ry(0.5, 1),
        qf.Rz(0.6, 2),
    ])
    assert s == quirk_url(circuit_to_quirk(circ))