Hi,
Several qiskit version ago, we were able to create the inverse of circuits from initialize() by issuing a command like this (used here: https://github.com/Qiskit/qiskit-tutorials/blob/master/community/hello_world/string_comparison.ipynb):
qc.initialize(desired_vector, qr).inverse() #invert the circuit
But with the latest qiskit it returns error that related to reset() function which is confusing because I cannot see why reset is used in the initialization.
---------------------------------------------------------------------------
QiskitError Traceback (most recent call last)
<ipython-input-12-8d561fe9b96b> in <module>
1 qc2 = QuantumCircuit(q)
----> 2 qc2.initialize(desired_vector, [q[0], q[1], q[2]]).inverse()
~/miniconda3/envs/qiskit_uot/lib/python3.7/site-packages/qiskit/circuit/instruction.py in inverse(self)
231 inverse_gate._definition = []
232 for inst, qargs, cargs in reversed(self._definition):
--> 233 inverse_gate._definition.append((inst.inverse(), qargs, cargs))
234 return inverse_gate
235
~/miniconda3/envs/qiskit_uot/lib/python3.7/site-packages/qiskit/circuit/instruction.py in inverse(self)
227 """
228 if not self.definition:
--> 229 raise QiskitError("inverse() not implemented for %s." % self.name)
230 inverse_gate = self.copy(name=self.name + '_dg')
231 inverse_gate._definition = []
QiskitError: 'inverse() not implemented for reset.'
What is the workaround for this? Noted that because quantum circuits are reversible, there should be a natural way to define a circuit from its inverse and vice versa.
circuit.initialize(...).inverse() produces an error related to reset() which should not be used in the creation of gates.
Run the notebook for string comparison here:
https://github.com/Qiskit/qiskit-tutorials/blob/master/community/hello_world/string_comparison.ipynb
We should be able to create an inverse of circuit based on the description of the circuit.
No idea.
I found out that initialize() use the reset() at the beginning. This makes the resulting circuit is not unitary anymore. I wonder why using reset() at all?
OPENQASM 2.0;
include "qelib1.inc";
qreg q94[3];
reset q94[0];
reset q94[1];
reset q94[2];
u3(1.95519310129054,0,0) q94[2];
u3(0,0,-0.312261443099564) q94[2];
This commit introduced reset in the initializer. https://github.com/Qiskit/qiskit-terra/commit/eda45e286144606fb27915817b8be7426d2e1b0a
Initialization is not a reversible operation. It is meant to initialize to the desired state from any state, not just the zero state. So a reset is necessary in case you insert the initialize instruction in the middle of a circuit.
The transpiler removes reset if it happens to be in the beginning of the circuit. So if you always use initialize in the beginning, pass it through transpile(circuit, optimization_level=1). (which invokes the RemoveResetInZeroState transpiler pass on it).
I do not agree that initialization is not a reversible operation. There are use cases of using circuits obtained from initialization whose inputs not necessarily all-zero states, but the circuits are generated on the all-zero states, such as those in the quantum-enhanced SVM papers.
Most helpful comment
I do not agree that initialization is not a reversible operation. There are use cases of using circuits obtained from initialization whose inputs not necessarily all-zero states, but the circuits are generated on the all-zero states, such as those in the quantum-enhanced SVM papers.