Environment
- Qiskit version: qiskit 2.3.0
- Python version: Python 3.10.18
- Operating system: Ubuntu 11.4.0-2ubuntu1~20.04
What is happening?
Non-unitary instruction, initialize() must fail early in inverse() with a CircuitError raised, not during gate construction, similarly to reset().
How can we reproduce the issue?
A small code to help with testing: (but it will happen with any circuit with initialize call)
import os
import numpy as np
import qiskit
from qiskit import QuantumCircuit
# THE MUTATION
def morphCircuit():
circuit = QuantumCircuit(4, 0)
circuit.initialize(0, circuit.qubits)
#circuit.reset(circuit.qubits)
circuit.h(0)
circuit.h(1)
circuit.h(2)
circuit.h(3)
return circuit.copy()
# MAIN::BLACK BOX
def main():
circuit = morphCircuit()
inverse = circuit.inverse()
if __name__ == "__main__":
main()
What should happen?
That is, instead of this error (with its non-informative trace):
Traceback (most recent call last):
File "/home/ubuntu/Quantum/Fuzzmorphiq/src/eqMutants/bugReport.py", line 22, in <module>
main()
File "/home/ubuntu/Quantum/Fuzzmorphiq/src/eqMutants/bugReport.py", line 19, in main
inverse = circuit.inverse()
File "/home/ubuntu/.local/lib/python3.10/site-packages/qiskit/circuit/quantumcircuit.py", line 1880, in inverse
instruction.replace(operation=instruction.operation.inverse(annotated=annotated))
File "/home/ubuntu/.local/lib/python3.10/site-packages/qiskit/circuit/instruction.py", line 421, in inverse
inverse_gate = Gate(name=name, num_qubits=self.num_qubits, params=self.params.copy())
File "/home/ubuntu/.local/lib/python3.10/site-packages/qiskit/circuit/gate.py", line 44, in __init__
super().__init__(name, num_qubits, 0, params, label=label)
File "/home/ubuntu/.local/lib/python3.10/site-packages/qiskit/circuit/instruction.py", line 102, in __init__
self.params = params # must be at last (other properties may be required for validation)
File "/home/ubuntu/.local/lib/python3.10/site-packages/qiskit/circuit/instruction.py", line 279, in params
self._params.append(self.validate_parameter(single_param))
File "/home/ubuntu/.local/lib/python3.10/site-packages/qiskit/circuit/gate.py", line 272, in validate_parameter
raise CircuitError(f"Invalid param type {type(parameter)} for gate {self.name}.")
qiskit.circuit.exceptions.CircuitError: "Invalid param type <class 'complex'> for gate initialize_dg."
it should not get to line 421 in qiskit/circuit/instruction.py and try to do inverse, but be tested before in def inverse(self, annotated: bool = False): and properly raise CircuitError(....), similarly to reset and as documentation suggests:
396 Raises:
397 CircuitError: if the instruction is not composite
398 and an inverse has not been implemented for it.
Any suggestions?
Checking the unitarity is likely too expensive. Maybe another kind of proxy check, as in reset. Another option is to let the inverse unfold as it is now, but to raise CircuitError before getting into the construction of initialize_dg.
Environment
What is happening?
Non-unitary instruction,
initialize()must fail early in inverse() with a CircuitError raised, not during gate construction, similarly toreset().How can we reproduce the issue?
A small code to help with testing: (but it will happen with any circuit with initialize call)
What should happen?
That is, instead of this error (with its non-informative trace):
it should not get to line 421 in qiskit/circuit/instruction.py and try to do inverse, but be tested before in
def inverse(self, annotated: bool = False):and properly raiseCircuitError(....), similarly to reset and as documentation suggests:Any suggestions?
Checking the unitarity is likely too expensive. Maybe another kind of proxy check, as in reset. Another option is to let the inverse unfold as it is now, but to raise
CircuitErrorbefore getting into the construction ofinitialize_dg.