def test_fail_on_bad_pragmas(): "Check that to_latex raises an error when pragmas are imbalanced." # missing END_LATEX_GATE_GROUP with pytest.raises(ValueError): _ = to_latex(Program(Pragma("LATEX_GATE_GROUP", [], 'foo'), X(0))) # missing LATEX_GATE_GROUP with pytest.raises(ValueError): _ = to_latex(Program(X(0), Pragma("END_LATEX_GATE_GROUP"))) # nested groups are not currently supported with pytest.raises(ValueError): _ = to_latex( Program(Pragma("LATEX_GATE_GROUP"), X(0), Pragma("LATEX_GATE_GROUP"), X(1), Pragma("END_LATEX_GATE_GROUP"), Pragma("END_LATEX_GATE_GROUP")))
def test_to_latex(): """A test to give full coverage of latex_generation and latex_config.""" qubits = range(3) p = Program() p.inst(X(qubits[0]), Y(qubits[0]), CZ(qubits[0], qubits[2]), SWAP(qubits[0], qubits[1]), MEASURE(qubits[0]), CNOT(qubits[2], qubits[0])) _ = to_latex(p) # Modify settings to access non-standard control paths. settings = get_default_settings() settings['gates']['AllocateQubitGate']['draw_id'] = True settings['gate_shadow'] = None _ = to_latex(p, settings) settings['control']['shadow'] = True _ = to_latex(p, settings)
def test_gate_group_pragma(): "Check that to_latex does not fail on LATEX_GATE_GROUP pragma." p = Program() p.inst(Pragma("LATEX_GATE_GROUP", [], 'foo'), X(0), X(0), Pragma("END_LATEX_GATE_GROUP"), X(1)) _ = to_latex(p)
def test_to_latex(): """A test to give full coverage of latex_generation.""" p = Program() p.inst(X(0), RX(1.0, 5), Y(0), CZ(0, 2), SWAP(0, 1), MEASURE(0, None), CNOT(2, 0), X(0).controlled(1), Y(0).dagger()) _ = to_latex(p) # Modify settings to access non-standard control paths. settings = DiagramSettings(impute_missing_qubits=True) _ = to_latex(p, settings) settings = DiagramSettings(abbreviate_controlled_rotations=True) _ = to_latex(p, settings) settings = DiagramSettings(label_qubit_lines=False) _ = to_latex(p, settings)
def test_unsupported_ops(): target = Label("target") base_prog = Program( Declare('reg1', 'BIT'), Declare('reg2', 'BIT'), H(0), JumpTarget(target), CNOT(0, 1)) bad_ops = [WAIT, Jump(target), MOVE(MemoryReference('reg1'), MemoryReference('reg2'))] assert to_latex(base_prog) for op in bad_ops: prog = base_prog + op with pytest.raises(ValueError): _ = to_latex(prog)
def functionToLatex(function): try: import pyquil except ImportError: print('Error - We delegate to Pyquil for Latex generation. Install Pyquil.') return if not hasCompiler('quil'): print('Error - We use XACC QuilCompiler to generate Latex. Install XACC-Rigetti plugin.') return from pyquil.latex import to_latex return to_latex(pyquil.quil.Program(getCompiler('quil').translate('', function)))
def plot_circuit(circuit): latex_diagram = to_latex(circuit) tmp_folder = mkdtemp() with open(tmp_folder + '/circuit.tex', 'w') as f: f.write(latex_diagram) proc = subprocess.Popen(['pdflatex', '-shell-escape', tmp_folder + '/circuit.tex'], cwd=tmp_folder) proc.communicate() image = plt.imread(tmp_folder + '/circuit.png') shutil.rmtree(tmp_folder) plt.axis('off') return plt.imshow(image)
def test_warn_on_pragma_with_trailing_measures(): "Check that to_latex warns when measurement alignment conflicts with gate group pragma." with pytest.warns(UserWarning): _ = to_latex( Program( Declare("ro", "BIT"), Pragma("LATEX_GATE_GROUP"), MEASURE(0, MemoryReference("ro")), Pragma("END_LATEX_GATE_GROUP"), MEASURE(1, MemoryReference("ro")), ))
def test_controlled_gate(): prog = Program(H(2).controlled(3)) # This is hardcoded, but better than nothing expected = r""" \begin{tikzcd} \lstick{\ket{q_{2}}} & \gate{H} & \qw \\ \lstick{\ket{q_{3}}} & \ctrl{-1} & \qw \end{tikzcd} """.strip().split() actual = to_latex(prog).split() start_idx = actual.index("\\begin{tikzcd}") assert expected == actual[start_idx : start_idx + len(expected)]
def run(filename): with open(filename, 'r') as infile: quil = infile.read() program = Program(quil) with open('circuit.tex', 'w') as outfile: outfile.write(to_latex(program)) qvm = get_qc('9q-square-qvm') print('Circuit output to circuit.tex') try: result = qvm.run(program) print(result) except: print( 'Warning: Program not run due to changing pyquil API. This will be updated in a future version' ) try: subprocess.check_output(['pdflatex', 'circuit.tex']) subprocess.check_output(['rm', 'circuit.log', 'circuit.aux']) print('Used pdflatex to output circuit to circuit.pdf') except: print('Warning: Install pdflatex to have circuit created as pdf file')
def test_fail_on_forked(): """Check that to_latex raises an exception on FORKED gates.""" p = Program() p.inst(RX(1.0, 0).forked(1, [2.0])) with pytest.raises(ValueError): _ = to_latex(p)
# job = execute(circ, backend, shots=10) # result = job.result() # counts = result.get_counts(circ) # # print(counts) ## personal testing # theoretical_probs = {'00000': 0.10139983459411607, '00001': 0.01735805702132643, '00010': 0.017358057021326437, '00011': 0.043801529890753046, '00100': 0.017358057021326423, '00101': 0.043801529890753046, '00110': 0.043801529890753046, '00111': 0.0017330570213264689, '01000': 0.017358057021326437, '01001': 0.04380152989075303, '01010': 0.043801529890753046, '01011': 0.0017330570213264678, '01100': 0.04380152989075303, '01101': 0.0017330570213264689, '01110': 0.0017330570213264678, '01111': 0.05942652989075301, '10000': 0.017358057021326437, '10001': 0.04380152989075307, '10010': 0.04380152989075308, '10011': 0.0017330570213264678, '10100': 0.04380152989075307, '10101': 0.0017330570213264689, '10110': 0.0017330570213264678, '10111': 0.05942652989075301, '11000': 0.04380152989075308, '11001': 0.0017330570213264695, '11010': 0.0017330570213264695, '11011': 0.05942652989075301, '11100': 0.0017330570213264654, '11101': 0.05942652989075301, '11110': 0.05942652989075301, '11111': 0.05933136172468943} # experimental_probs = {'00000': 0.0997, '00001': 0.0175, '00010': 0.015, '00011': 0.0418, '00100': 0.017, '00101': 0.0459, '00110': 0.0443, '00111': 0.0018, '01000': 0.0155, '01001': 0.0471, '01010': 0.0451, '01011': 0.0015, '01100': 0.039, '01101': 0.0018, '01110': 0.0014, '01111': 0.0611, '10000': 0.0201, '10001': 0.0449, '10010': 0.043, '10011': 0.0021, '10100': 0.0446, '10101': 0.0023, '10110': 0.0022, '10111': 0.0582, '11000': 0.0435, '11001': 0.0018, '11010': 0.0016, '11011': 0.0595, '11100': 0.0024, '11101': 0.0631, '11110': 0.0612, '11111': 0.054} # # # for key in theoretical_probs.keys(): # if key in experimental_probs.keys(): # print("present") # else: # print("not present") from pyquil.quil import Gate from pyquil.gates import H, CNOT, S from pyquil import Program, get_qc from pyquil.latex import to_latex from pyquil.api import local_qvm qvm = get_qc('3q-qvm') program = Program(H(2)) program += S(2) program += Gate.dagger(S(2)) print(to_latex(program))