Пример #1
0
 def test_teleport(self):
     """Test draw teleport circuit."""
     filename = self._get_resource_path('test_teleport.tex')
     qr = qiskit.QuantumRegister(3, 'q')
     cr = qiskit.ClassicalRegister(3, 'c')
     qc = qiskit.QuantumCircuit(qr, cr)
     # Prepare an initial state
     qc.u3(0.3, 0.2, 0.1, qr[0])
     # Prepare a Bell pair
     qc.h(qr[1])
     qc.cx(qr[1], qr[2])
     # Barrier following state preparation
     qc.barrier(qr)
     # Measure in the Bell basis
     qc.cx(qr[0], qr[1])
     qc.h(qr[0])
     qc.measure(qr[0], cr[0])
     qc.measure(qr[1], cr[1])
     # Apply a correction
     qc.z(qr[2]).c_if(cr, 1)
     qc.x(qr[2]).c_if(cr, 2)
     qc.measure(qr[2], cr[2])
     try:
         generate_latex_source(qc, filename)
         self.assertNotEqual(os.path.exists(filename), False)
     finally:
         if os.path.exists(filename):
             os.remove(filename)
Пример #2
0
 def test_normal_circuit(self):
     filename = self._get_resource_path('test_normal.tex')
     qc = self.random_circuit(5, 5, 3)
     try:
         generate_latex_source(qc, filename)
         self.assertNotEqual(os.path.exists(filename), False)
     finally:
         if os.path.exists(filename):
             os.remove(filename)
Пример #3
0
 def test_wide_circuit(self):
     """Test draw wide circuit."""
     filename = self._get_resource_path('test_wide.tex')
     qc = self.random_circuit(100, 1, 1)
     try:
         generate_latex_source(qc, filename)
         self.assertNotEqual(os.path.exists(filename), False)
     finally:
         if os.path.exists(filename):
             os.remove(filename)
Пример #4
0
def circuit_drawer(circuit, filename, directory=None):
    """Saves circuit to pdf

    Parameters:
        circuit (QuantumCircuit, DAGCircuit, Qasm): input circuit, better in Qasm format
        filename (str): filename to write pdf, file extension not needed
        directory (str): directory where the circuit will be saved
    """
    if isinstance(circuit, DAGCircuit):
        circuit = load_qasm_string(circuit.qasm())
    elif isinstance(circuit, str):
        circuit = load_qasm_string(circuit)
    elif isinstance(circuit, Qasm):
        circuit = load_qasm_string(circuit.parse())
    if directory is None:
        directory = ''
    generate_latex_source(circuit,
                          directory + filename + '.tex',
                          basis="id,u0,u1,u2,u3,x,y,z,h,s,sdg,t,tdg,rx,ry,rz,"
                          "cx,cy,cz,ch,crz,cu1,cu3,swap,ccx,cswap",
                          scale=0.8)
    if directory == '':
        cmd = ['pdflatex', '-interaction', 'nonstopmode', '%s.tex' % filename]
    else:
        cmd = [
            'pdflatex', '-interaction', 'nonstopmode', '-output-directory',
            directory,
            '%s.tex' % filename
        ]
    proc = subprocess.Popen(cmd, stdout=subprocess.DEVNULL)
    proc.communicate()

    retcode = proc.returncode
    if not retcode == 0:
        raise ValueError('Error {} executing command: {}'.format(
            retcode, ' '.join(cmd)))
    os.unlink('%s.log' % (directory + filename))
    os.unlink('%s.toc' % (directory + filename))
    os.unlink('%s.snm' % (directory + filename))
    os.unlink('%s.nav' % (directory + filename))
    os.unlink('%s.aux' % (directory + filename))