2

I am trying to work with parametrized circuits in QISKIT. I can simply create parameterized circuits, bind value(s) to parameter(s), and execute/run all instances (after binding values for parameters) on different backends like ibmq_manhattan, ibmq_paris, etc.

To dump the resulting circuits to QASM files, I use the “qasm()” method of the QuantumCircuit objects, and I see no problem. Here is a simple (random) QASM file that I create:

OPENQASM 2.0; include "qelib1.inc"; qreg q[3]; creg c[3]; h q[0]; h q[1]; h q[2]; barrier q[0],q[1],q[2]; rx(beta) q[0];

As you can see, the last line of the QASM file specifies a parameterized RX rotation, on qubit q[0] and the angle is (beta). As far as I am working with QuantumCircuit objects, I can deal with these parameters. However, after writing the circuit to a QASM file, I lose the functionality.

More specifically, when I try to load/read the QASM file and create a QuantumCircuit, I face the following error:

Code: circ = QuantumCircuit.from_qasm_file(filepath) Error: qiskit.qasm.exceptions.QasmError: "Argument 'beta' in expression cannot be found, line 9 file …

I also tried the following alternative (just in case): Code: circ = QuantumCircuit.from_qasm_str(qasm_str) and faced the same error. Here the “filepath” specifies the path to the QASM file, and I obtain the “qasm_str” via reading the QASM file.

I checked the source code of the “from_qasm_file()” module in QISKIT, as well as “Qasm” class; however, I did not find any additional input argument/parameter to specify these parameters to the parser.

I just need to read the (abovementioned) QASM file and re-create a QuantumCircuit object.

I would appreciate it a lot if someone can help me in this regard.

Thanks, Ramin

glS
  • 27,510
  • 7
  • 37
  • 125
Ramin
  • 21
  • 2

3 Answers3

2

Parameterized circuit is not supported by the OpenQASM2 specification https://arxiv.org/abs/1707.03429

The .qasm output is a bit of notation abuse. There are experimental ideas to support a more comprehensive circuit serialization format https://github.com/Qiskit/qiskit-terra/pull/5578

luciano
  • 6,084
  • 1
  • 13
  • 34
2

You can use qiskit-qasm3-import library

Once you have the QASM3 program, use :

from qiskit_qasm3_import import parse

And than

qc = parse(program)
Amir Naveh
  • 39
  • 7
0

You can use https://github.com/Qiskit/qiskit-qasm3-import

Once you have the QASM3 string program, use the following:

from qiskit_qasm3_import import parse
qc = parse(program)

If it was QASM2 you could use

qc = QuantumCircuit.from_qasm_str(program)
Ron Cohen
  • 1,512
  • 6
  • 24