def test_from_ast_to_dag(self): """Test Unroller.execute()""" ast = qasm.Qasm(filename=self._get_resource_path( 'example.qasm', Path.QASMS)).parse() dag_circuit = ast_to_dag(ast) expected_result = """\ OPENQASM 2.0; include "qelib1.inc"; qreg q[3]; qreg r[3]; creg c[3]; creg d[3]; h q[0]; h q[1]; h q[2]; cx q[0],r[0]; cx q[1],r[1]; cx q[2],r[2]; barrier q[0],q[1],q[2]; measure q[0] -> c[0]; measure q[1] -> c[1]; measure q[2] -> c[2]; measure r[0] -> d[0]; measure r[1] -> d[1]; measure r[2] -> d[2]; """ expected_dag = circuit_to_dag( QuantumCircuit.from_qasm_str(expected_result)) self.assertEqual(dag_circuit, expected_dag)
def test_from_ast_to_dag(self): """Test Unroller.execute()""" qasm_dir = os.path.join( os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'qasm') ast = qasm.Qasm(os.path.join(qasm_dir, 'example.qasm')).parse() dag_circuit = ast_to_dag(ast) expected_result = """\ OPENQASM 2.0; include "qelib1.inc"; qreg q[3]; qreg r[3]; creg c[3]; creg d[3]; h q[0]; h q[1]; h q[2]; cx q[0],r[0]; cx q[1],r[1]; cx q[2],r[2]; barrier q[0],q[1],q[2]; measure q[0] -> c[0]; measure q[1] -> c[1]; measure q[2] -> c[2]; measure r[0] -> d[0]; measure r[1] -> d[1]; measure r[2] -> d[2]; """ expected_dag = circuit_to_dag(QuantumCircuit.from_qasm_str(expected_result)) self.assertEqual(dag_circuit, expected_dag)
def _circuit_from_qasm(qasm): # pylint: disable=cyclic-import from qiskit.converters import ast_to_dag from qiskit.converters import dag_to_circuit ast = qasm.parse() dag = ast_to_dag(ast) return dag_to_circuit(dag)
def qasm_load(qasm: Qasm) -> QuantumCircuit: """ Factory OpenQASM src into QuantumCircuit using qiskit.qasm code. Parameters ---------- qasm : Qasm The Qasm object of source to load. Returns ------- QuantumCircuit The resulting QuantumCircuit. """ ast = qasm.parse() dag = ast_to_dag(ast) return dag_to_circuit(dag)
def _circuit_from_qasm(qasm): from qiskit.converters import ast_to_dag from qiskit.converters import dag_to_circuit ast = qasm.parse() dag = ast_to_dag(ast) return dag_to_circuit(dag)
def time_ast_to_circuit(self, *_): converters.ast_to_dag(self.qasm)
QGate["T"]*(3) with nocontrol QGate["not"](3) with controls=[+0] with nocontrol QGate["T"](3) with nocontrol QGate["H"](3) with nocontrol QGate["not"](4) with controls=[+3] with nocontrol QGate["T"]*(4) with nocontrol QGate["not"](4) with controls=[+2] with nocontrol QGate["T"](4) with nocontrol QGate["not"](4) with controls=[+3] with nocontrol QGate["not"](3) with controls=[+2] with nocontrol QGate["T"](3) with nocontrol QGate["not"](3) with controls=[+2] with nocontrol QGate["T"]*(3) with nocontrol QGate["H"](4) with nocontrol QGate["H"](3) with nocontrol QGate["not"](3) with controls=[+1] with nocontrol QGate["T"](3) with nocontrol QGate["not"](3) with controls=[+0] with nocontrol QGate["T"]*(3) with nocontrol QGate["not"](3) with controls=[+1] with nocontrol QGate["T"](3) with nocontrol QGate["not"](3) with controls=[+0] with nocontrol QGate["T"]*(3) with nocontrol QGate["H"](3) with nocontrol Outputs: 0:Qbit, 1:Qbit, 2:Qbit, 3:Qbit, 4:Qbit """ parsed: quippy.Start = quippy.parser().parse(text) dag = ast_to_dag(ast) dag_drawer(dag)
def test_dag_to_json(self): """Test DagUnroller with JSON backend.""" ast = qasm.Qasm( filename=self._get_resource_path('qasm/example.qasm')).parse() dag_circuit = ast_to_dag(ast) dag_unroller = DagUnroller(dag_circuit, JsonBackend()) json_circuit = dag_unroller.execute() expected_result = { 'operations': [{ 'qubits': [5], 'texparams': ['0.5 \\pi', '0', '\\pi'], 'name': 'U', 'params': [1.5707963267948966, 0.0, 3.141592653589793] }, { 'name': 'CX', 'qubits': [5, 2] }, { 'clbits': [2], 'name': 'measure', 'qubits': [2] }, { 'qubits': [4], 'texparams': ['0.5 \\pi', '0', '\\pi'], 'name': 'U', 'params': [1.5707963267948966, 0.0, 3.141592653589793] }, { 'name': 'CX', 'qubits': [4, 1] }, { 'clbits': [1], 'name': 'measure', 'qubits': [1] }, { 'qubits': [3], 'texparams': ['0.5 \\pi', '0', '\\pi'], 'name': 'U', 'params': [1.5707963267948966, 0.0, 3.141592653589793] }, { 'name': 'CX', 'qubits': [3, 0] }, { 'name': 'barrier', 'qubits': [3, 4, 5] }, { 'clbits': [5], 'name': 'measure', 'qubits': [5] }, { 'clbits': [4], 'name': 'measure', 'qubits': [4] }, { 'clbits': [3], 'name': 'measure', 'qubits': [3] }, { 'clbits': [0], 'name': 'measure', 'qubits': [0] }], 'header': { 'memory_slots': 6, 'qubit_labels': [['r', 0], ['r', 1], ['r', 2], ['q', 0], ['q', 1], ['q', 2]], 'n_qubits': 6, 'clbit_labels': [['d', 3], ['c', 3]] } } self.assertEqual(json_circuit, expected_result)