question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Generating controlled gate from QuantumCircuit results in AttributeError when circuit contains at least one explicit identity operator

See original GitHub issue

Environment

  • 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:closed
  • Created 2 years ago
  • Comments:13 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
jakelishmancommented, Feb 16, 2022

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.

1reaction
jakelishmancommented, Jan 25, 2022

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

qiskit.circuit.QuantumCircuit
Create a new circuit. A circuit is a list of instructions bound to some registers. Parameters. regs (list( Register ) ...
Read more >
Release notes — PennyLane 0.28.0 documentation
AdaptiveOptimizer takes an initial circuit and a collection of operators as input and adds a selected gate to the circuit at each optimization...
Read more >
Can I remove gates from a QuantumCircuit?
Quantum circuits have a data attribute, which is a list of all gates. You can remove elements from this as with any Python...
Read more >
The Ultimate Guide to a Quantum Computing Certification with ...
Using the CX-gate, we can create an XOR operation by placing two CX-gates in the circuit, with one on each input qubit, and...
Read more >
pyQuil Documentation - Rigetti Computing
The Rigetti Forest Software Development Kit includes pyQuil, the Rigetti Quil Compiler (quilc), and the Quantum. Virtual Machine (qvm).
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found