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

Creating a Parameterised Gate Using Unitary Matrix #11233

Closed
vandit2209 opened this issue Nov 11, 2023 · 3 comments
Closed

Creating a Parameterised Gate Using Unitary Matrix #11233

vandit2209 opened this issue Nov 11, 2023 · 3 comments
Labels
bug Something isn't working

Comments

@vandit2209
Copy link

vandit2209 commented Nov 11, 2023

Environment

  • Qiskit Terra version: 0.24.0
  • Python version: 3.8.5
  • Operating system: Linux-3.10.0-957.el7.x86_64-x86_64-with-glibc2.10
    OS printed using platform.platform

What is happening?

Error

RuntimeError                              Traceback (most recent call last)
File [~/.conda/envs/ibm_q/lib/python3.8/site-packages/qiskit/circuit/parameterexpression.py:474]/~/.conda/envs/ibm_q/lib/python3.8/site-packages/qiskit/circuit/parameterexpression.py:474, in ParameterExpression.__float__(self)
/.conda/envs/ibm_q/lib/python3.8/site-packages/qiskit/circuit/parameterexpression.py:473 try:
/.conda/envs/ibm_q/lib/python3.8/site-packages/qiskit/circuit/parameterexpression.py:474     return float(self._symbol_expr)
/.conda/envs/ibm_q/lib/python3.8/site-packages/qiskit/circuit/parameterexpression.py:475 # TypeError is for sympy, RuntimeError for symengine

File symengine_wrapper.pyx:1151, in symengine.lib.symengine_wrapper.Basic.__float__()

File symengine_wrapper.pyx:976, in symengine.lib.symengine_wrapper.Basic.n()

File symengine_wrapper.pyx:4346, in symengine.lib.symengine_wrapper.evalf()

RuntimeError: Symbol cannot be evaluated.

The above exception was the direct cause of the following exception:

TypeError                                 Traceback (most recent call last)
[/qrl/test.ipynb/qrl/test.ipynb) Cell 19 line 1
line=9 theta = Parameter("theta")
line=11 circuit.x(0)
line=12 cos_val = cos(float(theta) [/] 2)
line=13 sin_val = -1j * sin(float(theta) [/] 2)
line=14 circuit.data.insert(
line=15     0,
line=16     CircuitInstruction(
   (...)
line=26     ),
line=27 )

File [~/.conda/envs/ibm_q/lib/python3.8/site-packages/qiskit/circuit/parameterexpression.py:477/qrl/~/.conda/envs/ibm_q/lib/python3.8/site-packages/qiskit/circuit/parameterexpression.py:477), in ParameterExpression.__float__(self)
    /qrl/~/.conda/envs/ibm_q/lib/python3.8/site-packages/qiskit/circuit/parameterexpression.py:475) # TypeError is for sympy, RuntimeError for symengine
    /qrl/~/.conda/envs/ibm_q/lib/python3.8/site-packages/qiskit/circuit/parameterexpression.py:476) except (TypeError, RuntimeError) as exc:
--> /qrl/~/.conda/envs/ibm_q/lib/python3.8/site-packages/qiskit/circuit/parameterexpression.py:477)     raise TypeError(
    /qrl/~/.conda/envs/ibm_q/lib/python3.8/site-packages/qiskit/circuit/parameterexpression.py:478)         "ParameterExpression with unbound parameters ({}) "
    /qrl/~/.conda/envs/ibm_q/lib/python3.8/site-packages/qiskit/circuit/parameterexpression.py:479)         "cannot be cast to a float.".format(self.parameters)
    /qrl/~/.conda/envs/ibm_q/lib/python3.8/site-packages/qiskit/circuit/parameterexpression.py:480)     ) from exc

TypeError: ParameterExpression with unbound parameters ({Parameter(theta)}) cannot be cast to a float.

How can we reproduce the issue?

Code:

from qiskit import QuantumCircuit, QuantumRegister
from qiskit.quantum_info.operators import Operator
from qiskit.circuit import Parameter, CircuitInstruction
from math import cos, sin, pi

controls = QuantumRegister(2)
circuit = QuantumCircuit(controls)
# circuit.unitary()
# cx = Operator([[1, 0, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0], [0, 1, 0, 0]])
theta = Parameter("theta")

circuit.x(0)
cos_val = cos(float(theta) / 2)
sin_val = -1j * sin(float(theta) / 2)
circuit.data.insert(
    0,
    CircuitInstruction(
        operation=Operator(
            [
                [1, 0, 0, 0],
                [0, cos_val, 0, sin_val],
                [0, 0, 1, 0],
                [0, sin_val, 0, cos_val],
            ]
        ),
        qubits=[0, 1],
    ),
)
print(circuit)

What should happen?

Unbound Parameter error, originally initiated due to symengine

Any suggestions?

How do I create a Parameterised Gate from unitary matrix

@vandit2209 vandit2209 added the bug Something isn't working label Nov 11, 2023
@jakelishman
Copy link
Member

We do not support symbolic manipulation of dense matrices in Qiskit and have no plans to add such support. You can look at community-driven packages such as qiskit-symb for that kind of capability.

I would note that the gate you're trying to add is a controlled X rotation, which is (in theory) supported by the RXGate(theta).control() object or the QuantumCircuit.crx method. I think at the moment that there's a potential bug in that (see #10311 and linked issues). Your code is also using several low-level internal objects, and using them incorrectly:

  • You should not attempt to modify QuantumCircuit.data directly like that.
  • CircuitInstruction is a low-level object that you largely should not need to use.
  • The operation field of CircuitInstruction must be an instance of qiskit.circuit.Operation, not qiskit.quantum_info.Operator which is something entirely different.

@vandit2209
Copy link
Author

Thanks a lot for the suggestion and the explanation

@jakelishman
Copy link
Member

I'll close this issue as "solved" now, since I don't think there's anything more to do from the Qiskit side. Please feel free to re-open if there's more to discuss.

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

No branches or pull requests

2 participants