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

parameters in RXXGate #10697

Closed
Muzhou-Ma opened this issue Aug 23, 2023 · 7 comments
Closed

parameters in RXXGate #10697

Muzhou-Ma opened this issue Aug 23, 2023 · 7 comments
Labels
bug Something isn't working

Comments

@Muzhou-Ma
Copy link

Muzhou-Ma commented Aug 23, 2023

Environment

python version 3.10.11

What is happening?

I'm trying to give RXXGate a parameter phi when I encounter this error:
ParameterExpression with unbound parameters ({Parameter(a)}) cannot be cast to a float.

Here is my codes:

phi = Parameter('a')
qc = QuantumCircuit(4)
a = RXXGate(phi).control(2)
qc = qc.compose(a)
qc.draw()

However, it's interesting that everything works fine if the control method isn't used:

phi_1 = Parameter('a')
qc = QuantumCircuit(4)
a = RXXGate(theta = phi_1)
qc = qc.compose(a, [2, 3])
qc.draw()

which can give the result:

q_0: ───────────
                
q_1: ───────────
     ┌─────────┐
q_2: ┤0        ├
     │  Rxx(a) │
q_3: ┤1        ├
     └─────────┘

I'm wondering what I've done wrong and how to fix it?

Besides, is it possible for me to make this parameter trainable for optimization tasks like using gradient descend?

How can we reproduce the issue?

Perhaps making the Parameter class more flexible can help.

What should happen?

I suppose the gate should take this parameter and give it a random(or zero) initialization.

Any suggestions?

No response

@Muzhou-Ma Muzhou-Ma added the bug Something isn't working label Aug 23, 2023
@Raghav-Bell
Copy link
Contributor

You can stop the error even without assigning any value in control()
image

@Raghav-Bell
Copy link
Contributor

Raghav-Bell commented Aug 23, 2023

This error is raised from the following method.

def __float__(self):
try:
return float(self._symbol_expr)
# TypeError is for sympy, RuntimeError for symengine
except (TypeError, RuntimeError) as exc:
if self.parameters:
raise TypeError(
"ParameterExpression with unbound parameters ({}) "
"cannot be cast to a float.".format(self.parameters)

Check PR #10244 .

@jakelishman
Copy link
Member

I strongly suspect that this is the same root as #7326, because the internal definition of the multi-controlled $R_{xx}$ gate will use CU, which has issues in Qiskit at the moment. There's a potential fix in #9118 that I've just bumped up the priority list, because it keeps getting forgotten (by me - sorry!).

@Muzhou-Ma
Copy link
Author

Thanks for pointing out, hope to see the update soon!

@jakelishman
Copy link
Member

Sorry to come back to this after a long time with somewhat bad news: this isn't related to #9118, and the problem is that some of our controlled-gate synthesis code can't cope with parametrised gates in certain circumstances. In particular, we fail if asked to control a parametric RZGate with more than a single control state:

from qiskit.circuit import QuantumCircuit, Parameter

a = Parameter("a")
qc = QuantumCircuit(1)
qc.rz(a, 0)
gate = qc.to_gate()

gate.control(2)
<...>

File ~/code/qiskit/terra/qiskit/circuit/add_control.py:148, in control(operation, num_ctrl_qubits, label, ctrl_state)
    139     controlled_circ.mcry(
    140         gate.definition.data[0].operation.params[0],
    141         q_control,
   (...)
    145         use_basis_gates=True,
    146     )
    147 elif gate.name == "rz":
--> 148     controlled_circ.mcrz(
    149         gate.definition.data[0].operation.params[0],
    150         q_control,
    151         q_target[bit_indices[qargs[0]]],
    152         use_basis_gates=True,
    153     )
    154     continue
    155 elif gate.name == "p":

File ~/code/qiskit/terra/qiskit/circuit/library/standard_gates/multi_control_rotation_gates.py:381, in mcrz(self, lam, q_controls, q_target, use_basis_gates)
    378         self.append(CRZGate(lam), control_qubits + [target_qubit])
    379 else:
    380     cgate = _mcsu2_real_diagonal(
--> 381         RZGate(lam).to_matrix(),
    382         num_controls=len(control_qubits),
    383         use_basis_gates=use_basis_gates,
    384     )
    385     self.compose(cgate, control_qubits + [target_qubit], inplace=True)

<...>

TypeError: ParameterExpression with unbound parameters (dict_keys([Parameter(a)])) cannot be cast to a float.

That example is the root cause of your bug.

@ewinston, @alexanderivrii: do you two have any ideas of how we might go about modifying the synthesis to allow this?

@SimoneGasperini
Copy link
Contributor

This is related to issue #10311.

@ShellyGarion
Copy link
Member

closing this issue as #12752 is merged

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

5 participants