def test_latex_to_image() -> None: # TODO: Double check this circuit is correct 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'] latex = qf.circuit_to_latex(circ, order) qf.latex_to_image(latex) if os.environ.get("QF_VIZTEST"): qf.latex_to_image(latex).show()
def test_count(): circ = qf.Circuit() circ += qf.H(0) circ += qf.H(1) circ += qf.H(1) op_count = qf.count_operations(circ) assert op_count == {qf.H: 3} circ = qf.addition_circuit([0, 1, 2], [3, 4, 5], [6, 7]) op_count = qf.count_operations(circ) assert op_count == {qf.CNOT: 13, qf.CCNOT: 6}
def test_render_latex(): # TODO: Double check this circuit is correct 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'] latex = qf.circuit_to_latex(circ, order) qf.render_latex(latex)
def test_compile() -> None: circ0 = qf.addition_circuit([0], [1], [2, 3]) circ1 = qf.compile_circuit(circ0) assert qf.circuits_close(circ0, circ1) assert circ1.size() == 76 dagc = qf.DAGCircuit(circ1) assert dagc.depth(local=False) == 16 counts = qf.count_operations(dagc) assert counts[qf.ZPow] == 27 assert counts[qf.XPow] == 32 assert counts[qf.CZ] == 17
def test_addition_circuit(): # Two bit addition circuit circ = qf.addition_circuit([0, 1], [2, 3], [4, 5]) for c0 in range(0, 2): for a0 in range(0, 4): for a1 in range(0, 4): expected = a0 + a1 + c0 b0 = int_to_bitlist(a0, 2) b1 = int_to_bitlist(a1, 2) bc = [c0] bits = tuple(b0 + b1 + bc + [0]) state = np.zeros(shape=[2] * 6) state[bits] = 1 ket = qf.State(state) ket = circ.run(ket) bits = ket.measure() res = bits[[5, 2, 3]] res = bitlist_to_int(res) print(c0, a0, a1, expected, res) assert res == expected # Three bit addition circuit circ = qf.addition_circuit([0, 1, 2], [3, 4, 5], [6, 7]) for c0 in range(0, 2): for a0 in range(0, 8): for a1 in range(0, 8): expected = a0 + a1 + c0 b0 = int_to_bitlist(a0, 3) b1 = int_to_bitlist(a1, 3) bc = [c0] bits = tuple(b0 + b1 + bc + [0]) state = np.zeros(shape=[2] * 8) state[bits] = 1 ket = qf.State(state) ket = circ.run(ket) bits = ket.measure() res = bits[[7, 3, 4, 5]] res = bitlist_to_int(res) print(c0, a0, a1, expected, res) assert res == expected with pytest.raises(ValueError): qf.addition_circuit([0, 1, 2], [3, 4, 5, 6], [7, 8]) with pytest.raises(ValueError): qf.addition_circuit([0, 1, 2], [3, 4, 5], [6, 7, 8])
#!/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))
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
#!/usr/bin/env python # Copyright 2016-2018, Rigetti Computing # # 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: Example use of `quilc` to compile to Rigetti native gate set. """ import quantumflow as qf # Construct a rippler adder circuit to add two N-qubit registers N = 2 circ = qf.addition_circuit( list(range(N)), # first register list(range(N, 2 * N)), # second register [2 * N, 2 * N + 1]) # carry bits # Render pre-compilation circuit qf.circuit_to_image(circ).show() # Compile circuit compiler = qf.forest.get_compiler(circ.qubit_nb) prog = qf.forest.circuit_to_pyquil(circ) with qf.pyquil.local_qvm(): prog = compiler.quil_to_native_quil(prog) # Render post-compilation circuit circ = qf.forest.pyquil_to_circuit(prog) qf.circuit_to_image(circ).show()
sys.exit() if shutil.which('pdftocairo') is None: print('Failed: External dependency `pdftocairo` not found. ' 'Install `poppler`.') sys.exit() # Display Bell state preparation qf.circuit_to_image(qf.ghz_circuit([0, 1])).show() # 4-qubit GHZ state preparation qf.circuit_to_image(qf.ghz_circuit([0, 1, 2, 3])).show() # 4-qubit ripple add 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'] latex = qf.circuit_to_latex(circ, order) img = qf.render_latex(latex) img.show() # Visualize pyquil protoquil programs prog = Program() prog = prog.inst(Z(0)) prog = prog.inst(CNOT(0, 1)) prog = prog.inst(CZ(0, 1)) prog = prog.inst(CCNOT(0, 1, 2)) qf.forest.pyquil_to_image(prog).show()
import shutil import sys import quantumflow as qf if shutil.which("pdflatex") is None: print("Failed: External dependency `pdflatex` not found.") sys.exit() if shutil.which("pdftocairo") is None: print("Failed: External dependency `pdftocairo` not found. " "Install `poppler`.") sys.exit() # Display Bell state preparation qf.circuit_to_image(qf.ghz_circuit([0, 1])).show() # 4-qubit GHZ state preparation qf.circuit_to_image(qf.ghz_circuit([0, 1, 2, 3])).show() # 4-qubit ripple add 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" ] latex = qf.circuit_to_latex(circ, order) img = qf.latex_to_image(latex) img.show()