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_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()
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)
def test_stdgates_latex_to_image(gatename: str) -> None: if gatename not in qf.LATEX_GATESET: print("Latex not supported:", gatename) return gatet = qf.StdGate.cv_stdgates[gatename] gate = _randomize_gate(gatet) circ = qf.Circuit(gate) latex = qf.circuit_to_latex(circ) img = qf.latex_to_image(latex) if os.environ.get("QF_VIZTEST"): img.show()
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_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()
def test_pyquil_to_latex(): prog = pyquil.Program(BELL_STATE_MEASURE) circ = qf.forest.pyquil_to_circuit(prog) latex = qf.circuit_to_latex(circ) img = qf.render_latex(latex) assert img is not None
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()
def test_circuit_to_latex_error() -> None: circ = qf.Circuit([qf.RandomGate([0, 1, 2])]) with pytest.raises(NotImplementedError): qf.circuit_to_latex(circ)
def test_circuit_to_latex() -> None: qf.circuit_to_latex(qf.ghz_circuit(range(15)))
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()
def test_circuit_to_latex_error(): circ = qf.Circuit([qf.CPHASE01(0.4, 0, 1)]) with pytest.raises(NotImplementedError): qf.circuit_to_latex(circ)