Generating controlled gate from QuantumCircuit results in AttributeError when circuit contains at least one explicit identity operator
See original GitHub issueEnvironment
- Qiskit Terra version: 0.19.1 (Also tested this in 0.18.0 with same outcome)
- Python version: 3.9.7
- Operating system: CentOS (Also tested this on MacOS 12.0.1 with same outcome)
What is happening?
When you take a QuantumCircuit
where one of its constituent gates is explicitly the identity and you attempt to generate a controlled gate from this unitary using the control()
method, Qiskit raises the error:
AttributeError: 'NoneType' object has no attribute 'global_phase'
How can we reproduce the issue?
Examples of code that reproduce this error:
from qiskit.opflow.operator_globals import X, Y, Z, I
pauli_str_op = Z^Z^Z^I
pauli_str_op.to_circuit().control()
from qiskit import Aer, QuantumCircuit, QuantumRegister, AncillaRegister, ClassicalRegister
from qiskit.utils.algorithm_globals import algorithm_globals
from qiskit.utils.backend_utils import is_aer_provider
from qiskit.opflow.operator_globals import X, Y, Z, I
pauli_str_op = Z^Z^Z^I
qreg = QuantumRegister(4, 'q')
circ = QuantumCircuit(qreg)
circ.z(qreg[0])
circ.z(qreg[1])
circ.z(qreg[2])
circ.id(qreg[3])
circ.control()
This results in the following Traceback:
Traceback (most recent call last):
File "/Users/joelbierman/Desktop/Qiskit_Projects/control_gate_error.py", line 6, in <module>
pauli_str_op.to_circuit().control()
File "/Users/joelbierman/opt/anaconda3/envs/Qiskit_0280_pyscf2_ARM64/lib/python3.9/site-packages/qiskit/circuit/quantumcircuit.py", line 554, in control
controlled_gate = gate.control(num_ctrl_qubits, label, ctrl_state)
File "/Users/joelbierman/opt/anaconda3/envs/Qiskit_0280_pyscf2_ARM64/lib/python3.9/site-packages/qiskit/circuit/gate.py", line 119, in control
return add_control(self, num_ctrl_qubits, label, ctrl_state)
File "/Users/joelbierman/opt/anaconda3/envs/Qiskit_0280_pyscf2_ARM64/lib/python3.9/site-packages/qiskit/circuit/add_control.py", line 59, in add_control
cgate = control(operation, num_ctrl_qubits=num_ctrl_qubits, label=label, ctrl_state=ctrl_state)
File "/Users/joelbierman/opt/anaconda3/envs/Qiskit_0280_pyscf2_ARM64/lib/python3.9/site-packages/qiskit/circuit/add_control.py", line 113, in control
unrolled_gate = _unroll_gate(operation, basis_gates=basis)
File "/Users/joelbierman/opt/anaconda3/envs/Qiskit_0280_pyscf2_ARM64/lib/python3.9/site-packages/qiskit/circuit/add_control.py", line 268, in _unroll_gate
opqc = dag_to_circuit(unroller.run(dag))
File "/Users/joelbierman/opt/anaconda3/envs/Qiskit_0280_pyscf2_ARM64/lib/python3.9/site-packages/qiskit/transpiler/passes/basis/unroller.py", line 113, in run
unrolled_dag = self.run(decomposition) # recursively unroll ops
File "/Users/joelbierman/opt/anaconda3/envs/Qiskit_0280_pyscf2_ARM64/lib/python3.9/site-packages/qiskit/transpiler/passes/basis/unroller.py", line 76, in run
raise QiskitError(
qiskit.exceptions.QiskitError: "Error decomposing node of instruction 'id': 'NoneType' object has no attribute 'global_phase'. Unable to define instruction 'id' in the given basis."
What should happen?
Controlled unitary of QuantumCircuit
is generated, regardless of what unitary gates make up the circuit.
Any suggestions?
This seems to be related to how Qiskit handles generating controlled unitaries from circuits that explicitly contain at least one identity operator. This of course does not happen if the identity implicitly acts on one of the qubits since this would, by definition, break the control()
method for all circuits. For example, the following code does not raise this error:
from qiskit import Aer, QuantumCircuit, QuantumRegister, AncillaRegister, ClassicalRegister
from qiskit.utils.algorithm_globals import algorithm_globals
from qiskit.utils.backend_utils import is_aer_provider
from qiskit.opflow.operator_globals import X, Y, Z, I
pauli_str_op = Z^Z^Z^I
qreg = QuantumRegister(4, 'q')
circ = QuantumCircuit(qreg)
circ.z(qreg[0])
circ.z(qreg[1])
circ.z(qreg[2])
# nothing acts on qubit 3: the identity is implicit.
circ.control()
Issue Analytics
- State:
- Created 2 years ago
- Comments:13 (7 by maintainers)
Top GitHub Comments
I think the remaining problems raised by this issue are just duplicates of the ones that Kevin’s raised, so I’ll close this now.
It’s probably best to leave the issue open, since the other part of it is still going to be an issue after this, until we’re able to realign
IGate
to be more what most people expect. But it’s good that Julien’s patch will fix your actual issue.