Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gate.control method throwing error #10787

Closed
sharipr opened this issue Sep 6, 2023 · 3 comments · Fixed by #10850
Closed

Gate.control method throwing error #10787

sharipr opened this issue Sep 6, 2023 · 3 comments · Fixed by #10850
Assignees
Labels
bug Something isn't working

Comments

@sharipr
Copy link

sharipr commented Sep 6, 2023

Environment

  • Qiskit Terra version: 0.24.0
  • Python version: Python 3.9.6

What is happening?

Given a quantum circuit object, we execute the to_gate() method and attempt to call the Gate.control() method with appropriate parameters and sometimes observe the following error: *** UnboundLocalError: local variable 'mat2' referenced before assignment

How can we reproduce the issue?

from qiskit.extensions import UnitaryGate
from qiskit import QuantumCircuit, QuantumRegister

# initialize a unitary matrix

A = np.array([[ 0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  1.+0.j,  0.+0.j,  0.+0.j,
         0.+0.j],
       [ 0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  1.+0.j,  0.+0.j,
         0.+0.j],
       [ 0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j, -1.+0.j,
         0.+0.j],
       [ 0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,
        -1.+0.j],
       [ 1.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,
         0.+0.j],
       [ 0.+0.j,  1.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,
         0.+0.j],
       [ 0.+0.j,  0.+0.j, -1.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,
         0.+0.j],
       [ 0.+0.j,  0.+0.j,  0.+0.j, -1.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,
         0.+0.j]])

gate = UnitaryGate(A)
qc = QuantumCircuit(3)
qc.append(gate,[0,1,2])

# This happens in a a different function than the operations above

CU = qc.to_gate().control(6,28)

What should happen?

I would assume that the error is not thrown

Any suggestions?

The error appears to come from the file qsd.py, and in particular because the list ind2q (line 234) has only one element. The for loop that starts on line 242 is thus never executed and the the variable mat2 is never initialized.

@sharipr sharipr added the bug Something isn't working label Sep 6, 2023
@jakelishman
Copy link
Member

I believe this is a duplicate of #10036, so the fix should have been released in Terra 0.24.1. Could you try upgrading and see if the problem persists?

@sharipr sharipr changed the title Gate.control method throwing unexpected error Gate.control method throwing error Sep 6, 2023
@jakelishman
Copy link
Member

Oh no, this still seems to be present on main despite #10126. @ewinston, please can you look at this?

@sharipr
Copy link
Author

sharipr commented Sep 13, 2023

@jakelishman @ewinston I see the lines of code that are meant to fix this (from 2 months ago). Lines 246 and 247 are:

if not ind2q:
        return ccirc

I could be wrong but it also seems like if the list ind2q has just one element, the for loop that comes next will not be executed and the variable mat2 will never be initialized, thus throwing the same error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants