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

Invalid param type <class 'numpy.ndarray'> for gate unitary*1 #11990

Closed
jiannanWang opened this issue Mar 11, 2024 · 3 comments · Fixed by #12986
Closed

Invalid param type <class 'numpy.ndarray'> for gate unitary*1 #11990

jiannanWang opened this issue Mar 11, 2024 · 3 comments · Fixed by #12986
Labels
bug Something isn't working

Comments

@jiannanWang
Copy link

Environment

  • Qiskit version: 1.0.2
  • Python version: 3.11.8
  • Operating system: Ubuntu 22.04.3 LTS

What is happening?

Running the below code triggered a qiskit.circuit.exceptions.CircuitError: "Invalid param type <class 'numpy.ndarray'> for gate unitary*1." I found this bug happens for some other Gates as well. Below is a minimal reproduction example using CUGate.

How can we reproduce the issue?

from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister
from qiskit.circuit.library.standard_gates import CUGate

qr = QuantumRegister(2, name='qr')
cr = ClassicalRegister(2, name='cr')
qc = QuantumCircuit(qr, cr, name='qc')

qc.append(CUGate(0, 0, 0, 0).power(1).repeat(1), qargs=qr)

What should happen?

Traceback (most recent call last):
  File "/mnt/tests/reproduction/reproduce_invalid_param_type.py", line 8, in <module>
    qc.append(CUGate(0, 0, 0, 0).power(1).repeat(1), qargs=qr)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/anaconda3/envs/qiskit1.0.2/lib/python3.11/site-packages/qiskit/circuit/instruction.py", line 591, in repeat
    instruction = self._return_repeat(n)
                  ^^^^^^^^^^^^^^^^^^^^^^
  File "/root/anaconda3/envs/qiskit1.0.2/lib/python3.11/site-packages/qiskit/circuit/gate.py", line 87, in _return_repeat
    return Gate(name=f"{self.name}*{exponent}", num_qubits=self.num_qubits, params=self.params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/anaconda3/envs/qiskit1.0.2/lib/python3.11/site-packages/qiskit/circuit/gate.py", line 46, in __init__
    super().__init__(name, num_qubits, 0, params, label=label, duration=duration, unit=unit)
  File "/root/anaconda3/envs/qiskit1.0.2/lib/python3.11/site-packages/qiskit/circuit/instruction.py", line 108, in __init__
    self.params = params  # must be at last (other properties may be required for validation)
    ^^^^^^^^^^^
  File "/root/anaconda3/envs/qiskit1.0.2/lib/python3.11/site-packages/qiskit/circuit/instruction.py", line 287, in params
    self._params.append(self.validate_parameter(single_param))
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/anaconda3/envs/qiskit1.0.2/lib/python3.11/site-packages/qiskit/circuit/gate.py", line 242, in validate_parameter
    raise CircuitError(f"Invalid param type {type(parameter)} for gate {self.name}.")
qiskit.circuit.exceptions.CircuitError: "Invalid param type <class 'numpy.ndarray'> for gate unitary*1."

Any suggestions?

No response

@jiannanWang jiannanWang added the bug Something isn't working label Mar 11, 2024
@shailesh
Copy link

@jiannanWang To work around maybe you can manually create a composite gate by repeating the gate's unitary matrix

@Randl
Copy link
Contributor

Randl commented Jun 3, 2024

I think the fix should be to replace

return Gate(name=f"{self.name}*{exponent}", num_qubits=self.num_qubits, params=self.params)

with

return self.__init__(name=f"{self.name}*{exponent}", num_qubits=self.num_qubits, params=self.params)

However, even though the UnitaryGate inherits from Gate, it doesn't include parameter name; so this fix should also involve unification of all Gate subclasses by parameters.

@Randl
Copy link
Contributor

Randl commented Jun 3, 2024

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