1
$\begingroup$

Is there a way to save an instance of qiskit's QuantumCircuit in some format when the circuit has still some free parameters to be evaluated? I have tried using the qasm format but it seems it does not tolerate non-evaluated parameters.

$\endgroup$

1 Answer 1

0
$\begingroup$

Yes, there is - QPY serialization. It is useful for the case you have depicted but also for many other cases where the QASM format imposes restrictions.

An example:

Creating the circuit and dumping it to a qpy file:

from qiskit import QuantumCircuit, qpy
from qiskit.circuit import Parameter

theta = Parameter("$\Theta$")
qc = QuantumCircuit(1)
qc.ry(theta, 0)

with open("qc.qpy", "wb") as qpy_file_write:
    qpy.dump(qc, qpy_file_write)

Loading the circuit and assigning a value to $\theta$:

import numpy as np
from qiskit import qpy

with open("qc.qpy", "rb") as qpy_file_read:
    qc_loaded = qpy.load(qpy_file_read)[0]

qc_loaded = qc_loaded.bind_parameters([np.pi/2])
qc_loaded.draw("mpl")

Displays:

enter image description here

$\endgroup$
2
  • $\begingroup$ Thanks a lot for the answer. It could be great if you could also spend a couple of works on how to then recover the parameter and bound it to some value, is that possible? $\endgroup$ Feb 25 at 9:12
  • $\begingroup$ I have edited my answer above with an example. If you need to define several parameters it can be done using ParameterVector - qiskit.org/documentation/stubs/…. Let me know if you need anything more. $\endgroup$
    – Ohad
    Feb 25 at 9:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.