Ejemplo n.º 1
0
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()
Ejemplo n.º 2
0
def _print_circuit_identity(name,
                            circ0,
                            circ1,
                            min_col_width=0,
                            col_sep=5,
                            left_margin=8):

    print()
    print("", name)
    print()

    circ0 = qf.Circuit(circ0)
    circ1 = qf.Circuit(circ1)

    gates0 = qf.circuit_to_diagram(circ0, qubit_labels=False).splitlines()
    gates1 = qf.circuit_to_diagram(circ1, qubit_labels=False).splitlines()

    for gate0, gate1 in zip_longest(gates0, gates1, fillvalue=""):
        line = (" " * col_sep).join(
            [gate0.ljust(min_col_width),
             gate1.ljust(min_col_width)])
        line = (" " * left_margin) + line
        line = line.rstrip()
        print(line)

    print()
    print()
Ejemplo n.º 3
0
def _print_circuit_identity(name,
                            circ0,
                            circ1,
                            min_col_width=0,
                            col_sep=5,
                            left_margin=8):
    print("# ", name)

    circ0 = Circuit(circ0)
    circ1 = Circuit(circ1)

    gates0 = circuit_to_diagram(circ0, qubit_labels=False).splitlines()
    gates1 = circuit_to_diagram(circ1, qubit_labels=False).splitlines()

    for gate0, gate1 in zip_longest(gates0, gates1, fillvalue=""):
        line = (" " * col_sep).join(
            [gate0.ljust(min_col_width),
             gate1.ljust(min_col_width)])
        line = (" " * left_margin) + line
        line = line.rstrip()
        print(line)

    print()
    print()

    concrete = {name: np.random.uniform() for name in syms.values()}
    circ0f = circ0.resolve(concrete)
    circ1f = circ1.resolve(concrete)
    assert gates_close(circ0f.asgate(), circ1f.asgate())
Ejemplo n.º 4
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
Ejemplo n.º 5
0
def test_circuit_diagram() -> None:
    circ = qf.Circuit()
    circ += qf.Givens(pi, 1, 0)  # Not yet supported in latex
    diag = qf.circuit_to_diagram(circ)
    print()
    print(diag)
    print()
Ejemplo n.º 6
0
def test_gate2_to_diagrams() -> None:
    circ = qf.Circuit()

    circ += qf.CNot(0, 1)
    circ += qf.CZ(0, 1)
    circ += qf.CV(0, 1)
    circ += qf.CV_H(0, 1)
    circ += qf.CH(0, 1)
    circ += qf.Swap(0, 1)
    circ += qf.ISwap(0, 1)

    circ += qf.CNot(0, 2)
    circ += qf.CZ(0, 2)
    circ += qf.CV(0, 2)
    circ += qf.CV_H(0, 2)
    circ += qf.CH(0, 2)
    circ += qf.Swap(0, 2)
    circ += qf.ISwap(0, 2)

    circ += qf.CNot(2, 1)
    circ += qf.CZ(2, 1)
    circ += qf.CV(2, 1)
    circ += qf.CV_H(2, 1)
    circ += qf.CH(2, 1)
    circ += qf.Swap(2, 1)
    circ += qf.ISwap(2, 1)

    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()
Ejemplo n.º 7
0
def test_gate_symbolic_params() -> None:
    theta = Symbol("θ")

    gate0 = qf.Rz(theta, 1)
    assert str(gate0) == "Rz(θ) 1"

    gate1 = gate0**4
    assert str(gate1) == "Rz(4*θ) 1"

    circ = qf.Circuit([gate0, gate1])
    print(circ)
    diag = qf.circuit_to_diagram(circ)
    assert diag == "1: ───Rz(θ)───Rz(4*θ)───\n"

    gate2 = gate0.resolve({"θ": 2})
    assert gate2.param("theta") == 2.0

    with pytest.raises(KeyError):
        _ = gate2.param("asldfh")
Ejemplo n.º 8
0
def test_cirq_to_circuit2() -> None:

    q0 = cq.GridQubit(0, 0)
    q1 = cq.GridQubit(1, 0)

    def basic_circuit(meas=False):  # type: ignore
        sqrt_x = cq.X**0.5
        yield cq.X(q0)**0.5, sqrt_x(q1)
        yield cq.CZ(q0, q1)
        yield sqrt_x(q0), sqrt_x(q1)
        if meas:
            yield cq.measure(q0, key="q0"), cq.measure(q1, key="q1")

    cqc = cq.Circuit()
    cqc.append(basic_circuit())

    print()
    print(cqc)

    circ = cirq_to_circuit(cqc)
    print()
    print(qf.circuit_to_diagram(circ))
Ejemplo n.º 9
0
#!/usr/bin/env python
"""
QuantumFlow: Examples of compiling circuits to native gates
"""

import quantumflow as qf

example_circuits = [
    ["3-qubit addition", qf.addition_circuit([0, 1, 2], [3, 4, 5], [6, 7])],
    ["7-qubit QFT", qf.Circuit(qf.QFTGate([0, 1, 2, 3, 4, 5, 6, 7]).decompose())],
]

for title, example in example_circuits:
    print()
    print(title)
    print(qf.circuit_to_diagram(example))
    print("Gate count:", example.size())

    print()
    print("Simplified circuit")
    circ = qf.compile_circuit(example)
    print(qf.circuit_to_diagram(circ, transpose=True))

    qf.circuit_to_image(circ).show()
    dagc = qf.DAGCircuit(circ)
    print("Gate depth", dagc.depth(local=False))
    print("Operation count", qf.count_operations(dagc))
Ejemplo n.º 10
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()
Ejemplo n.º 11
0
def test_circuit_to_diagram() -> None:
    circ = qf.addition_circuit(
        ["a[0]", "a[1]", "a[2]", "a[3]"],
        ["b[0]", "b[1]", "b[2]", "b[3]"],
        ["cin", "cout"],
    )
    order = [
        "cin",
        "a[0]",
        "b[0]",
        "a[1]",
        "b[1]",
        "a[2]",
        "b[2]",
        "a[3]",
        "b[3]",
        "cout",
    ]
    # order = ['cin', 'a0', 'a1', 'a2', 'a3', 'b0', 'b1', 'b2', 'b3','cout']

    text = qf.circuit_to_diagram(circ, order)

    res = """\
cin:  ───────X───●───────────────────────────────────────────────────────────────────●───X───●───
             │   │                                                                   │   │   │   
a[0]: ───●───┼───┼───────────────────●───X───●───X───●───────────────────────────────┼───┼───┼───
         │   │   │                   │   │   │   │   │                               │   │   │   
b[0]: ───X───┼───┼───────────────────┼───●───┼───●───┼───X───────────────────────────┼───┼───┼───
             │   │                   │   │   │   │   │   │                           │   │   │   
a[1]: ───●───┼───┼───────────●───X───X───●───┼───●───X───●───X───●───────────────────┼───┼───┼───
         │   │   │           │   │           │               │   │                   │   │   │   
b[1]: ───X───┼───┼───────────┼───●───────────┼───────────────●───┼───X───────────────┼───┼───┼───
             │   │           │   │           │               │   │   │               │   │   │   
a[2]: ───●───┼───┼───●───X───X───●───────────┼───────────────●───X───●───X───●───────┼───┼───┼───
         │   │   │   │   │                   │                           │   │       │   │   │   
b[2]: ───X───┼───┼───┼───●───────────────────┼───────────────────────────●───┼───X───┼───┼───┼───
             │   │   │   │                   │                           │   │   │   │   │   │   
a[3]: ───●───●───X───X───●───────────────────┼───────────────────────────●───X───●───X───●───┼───
         │       │                           │                                       │       │   
b[3]: ───X───────●───────────────────────────┼───────────────────────────────────────●───────X───
                                             │                                                   
cout: ───────────────────────────────────────X───────────────────────────────────────────────────
"""  # noqa: W291, E501

    print()
    print(text)

    assert res == text

    ascii_circ = qf.circuit_to_diagram(circ, order, use_unicode=False)
    ascii_res = """\
cin:  -------X---@-------------------------------------------------------------------@---X---@---
             |   |                                                                   |   |   |   
a[0]: ---@---+---+-------------------@---X---@---X---@-------------------------------+---+---+---
         |   |   |                   |   |   |   |   |                               |   |   |   
b[0]: ---X---+---+-------------------+---@---+---@---+---X---------------------------+---+---+---
             |   |                   |   |   |   |   |   |                           |   |   |   
a[1]: ---@---+---+-----------@---X---X---@---+---@---X---@---X---@-------------------+---+---+---
         |   |   |           |   |           |               |   |                   |   |   |   
b[1]: ---X---+---+-----------+---@-----------+---------------@---+---X---------------+---+---+---
             |   |           |   |           |               |   |   |               |   |   |   
a[2]: ---@---+---+---@---X---X---@-----------+---------------@---X---@---X---@-------+---+---+---
         |   |   |   |   |                   |                           |   |       |   |   |   
b[2]: ---X---+---+---+---@-------------------+---------------------------@---+---X---+---+---+---
             |   |   |   |                   |                           |   |   |   |   |   |   
a[3]: ---@---@---X---X---@-------------------+---------------------------@---X---@---X---@---+---
         |       |                           |                                       |       |   
b[3]: ---X-------@---------------------------+---------------------------------------@-------X---
                                             |                                                   
cout: ---------------------------------------X---------------------------------------------------
"""  # noqa: W291, E501

    print()
    print(ascii_res)
    assert ascii_res == ascii_circ

    transposed = """\
│ │ │ │ │ │ │ │ │ │
│ │ │ │ │ │ │ │ │ │
│ │ │ │ │ │ │ │ │ │
│ ●─X ●─X ●─X ●─X │
│ │ │ │ │ │ │ │ │ │
│ │ │ │ │ │ │ │ │ │
│ │ │ │ │ │ │ │ │ │
X─┼─┼─┼─┼─┼─┼─● │ │
│ │ │ │ │ │ │ │ │ │
│ │ │ │ │ │ │ │ │ │
│ │ │ │ │ │ │ │ │ │
●─┼─┼─┼─┼─┼─┼─X─● │
│ │ │ │ │ │ │ │ │ │
│ │ │ │ │ │ │ │ │ │
│ │ │ │ │ │ │ │ │ │
│ │ │ │ │ ●─┼─X │ │
│ │ │ │ │ │ │ │ │ │
│ │ │ │ │ │ │ │ │ │
│ │ │ │ │ │ │ │ │ │
│ │ │ │ │ X─●─● │ │
│ │ │ │ │ │ │ │ │ │
│ │ │ │ │ │ │ │ │ │
│ │ │ │ │ │ │ │ │ │
│ │ │ ●─┼─X │ │ │ │
│ │ │ │ │ │ │ │ │ │
│ │ │ │ │ │ │ │ │ │
│ │ │ │ │ │ │ │ │ │
│ │ │ X─●─● │ │ │ │
│ │ │ │ │ │ │ │ │ │
│ │ │ │ │ │ │ │ │ │
│ │ │ │ │ │ │ │ │ │
│ ●─┼─X │ │ │ │ │ │
│ │ │ │ │ │ │ │ │ │
│ │ │ │ │ │ │ │ │ │
│ │ │ │ │ │ │ │ │ │
│ X─●─● │ │ │ │ │ │
│ │ │ │ │ │ │ │ │ │
│ │ │ │ │ │ │ │ │ │
│ │ │ │ │ │ │ │ │ │
│ ●─┼─┼─┼─┼─┼─┼─┼─X
│ │ │ │ │ │ │ │ │ │
│ │ │ │ │ │ │ │ │ │
│ │ │ │ │ │ │ │ │ │
│ X─●─● │ │ │ │ │ │
│ │ │ │ │ │ │ │ │ │
│ │ │ │ │ │ │ │ │ │
│ │ │ │ │ │ │ │ │ │
│ ●─┼─X │ │ │ │ │ │
│ │ │ │ │ │ │ │ │ │
│ │ │ │ │ │ │ │ │ │
│ │ │ │ │ │ │ │ │ │
│ │ X─● │ │ │ │ │ │
│ │ │ │ │ │ │ │ │ │
│ │ │ │ │ │ │ │ │ │
│ │ │ │ │ │ │ │ │ │
│ │ │ X─●─● │ │ │ │
│ │ │ │ │ │ │ │ │ │
│ │ │ │ │ │ │ │ │ │
│ │ │ │ │ │ │ │ │ │
│ │ │ ●─┼─X │ │ │ │
│ │ │ │ │ │ │ │ │ │
│ │ │ │ │ │ │ │ │ │
│ │ │ │ │ │ │ │ │ │
│ │ │ │ X─● │ │ │ │
│ │ │ │ │ │ │ │ │ │
│ │ │ │ │ │ │ │ │ │
│ │ │ │ │ │ │ │ │ │
│ │ │ │ │ X─●─● │ │
│ │ │ │ │ │ │ │ │ │
│ │ │ │ │ │ │ │ │ │
│ │ │ │ │ │ │ │ │ │
│ │ │ │ │ ●─┼─X │ │
│ │ │ │ │ │ │ │ │ │
│ │ │ │ │ │ │ │ │ │
│ │ │ │ │ │ │ │ │ │
│ │ │ │ │ │ X─● │ │
│ │ │ │ │ │ │ │ │ │
│ │ │ │ │ │ │ │ │ │
│ │ │ │ │ │ │ │ │ │
●─┼─┼─┼─┼─┼─┼─X─● │
│ │ │ │ │ │ │ │ │ │
│ │ │ │ │ │ │ │ │ │
│ │ │ │ │ │ │ │ │ │
X─┼─┼─┼─┼─┼─┼─● │ │
│ │ │ │ │ │ │ │ │ │
│ │ │ │ │ │ │ │ │ │
│ │ │ │ │ │ │ │ │ │
●─┼─┼─┼─┼─┼─┼─┼─X │
│ │ │ │ │ │ │ │ │ │
│ │ │ │ │ │ │ │ │ │
│ │ │ │ │ │ │ │ │ │
"""  # noqa

    text = qf.circuit_to_diagram(circ,
                                 order,
                                 transpose=True,
                                 qubit_labels=False)
    print(text)
    assert text == transposed
Ejemplo n.º 12
0
#!/usr/bin/env python

# Copyright 2019-, Gavin E. Crooks and the QuantumFlow contributors
#
# This source code is licensed under the Apache License, Version 2.0 found in
# the LICENSE.txt file in the root directory of this source tree.
"""
QuantumFlow Examples:
    Demonstration of decomposing a CSwap into CZ and 1-qubit gates
"""
import quantumflow as qf

circ0 = qf.Circuit([qf.CSwap(0, 1, 2)])

translators = [
    qf.translate_cswap_to_ccnot,
    qf.translate_ccnot_to_cnot,
    qf.translate_cnot_to_cz,
]
circ1 = qf.circuit_translate(circ0, translators)

assert circ1.size() == 33
print(qf.circuit_to_diagram(circ1))