Qiskit-terra: `circuit.initialize()` is using `reset` which makes the circuit not reversible and thus a bug!

Created on 9 Jul 2019  路  4Comments  路  Source: Qiskit/qiskit-terra



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.

Information

  • Qiskit Terra version: 0.8.2
  • Python version: 3.7
  • Operating system: mac osx

What is the current behavior?

circuit.initialize(...).inverse() produces an error related to reset() which should not be used in the creation of gates.

Steps to reproduce the problem

Run the notebook for string comparison here:
https://github.com/Qiskit/qiskit-tutorials/blob/master/community/hello_world/string_comparison.ipynb

What is the expected behavior?

We should be able to create an inverse of circuit based on the description of the circuit.

Suggested solutions

No idea.

question

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.

All 4 comments

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];

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kdk picture kdk  路  4Comments

jaygambetta picture jaygambetta  路  6Comments

ewinston picture ewinston  路  3Comments

we-taper picture we-taper  路  3Comments

ajavadia picture ajavadia  路  5Comments