Given a circuit, if one wishes to add a gate, it is done through a line like the one below
mycircuit.cx(0, 1)
Here, we have added a control-X gate on the 0 and 1 qubits. This is probably a Python question but how can one do this given the string 'cx'
and integers 0
and 1
i.e. without manually typing them?
I tried using https://stackoverflow.com/questions/3061/calling-a-function-of-a-module-by-using-its-name-a-string but getattr did not work for me.
from qiskit import QuantumCircuit
circ = QuantumCircuit(2)
circ.draw()
a = ('cx', 0, 1)
circ = getattr(circ, a[0])(a[1], a[2])
circ.draw()
getattr
?getattr(mycircuit, 'cx')(0, 1)
works for me in this case. $\endgroup$