-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Help to transpile a parameterized circuit with more than 3 controls #13192
Comments
Apparently, it's because they haven't implemented the def control(
operation: Gate | ControlledGate,
num_ctrl_qubits: int | None = 1,
label: str | None = None,
ctrl_state: str | int | None = None,
) -> ControlledGate:
"""Return controlled version of gate using controlled rotations. This function
first checks the name of the operation to see if it knows of a method from which
to generate a controlled version. Currently, these are ``x``, ``rx``, ``ry``, and ``rz``.
If a method is not directly known, it calls the unroller to convert to `u1`, `u3`,
and `cx` gates.
Args:
operation: The gate used to create the ControlledGate.
num_ctrl_qubits: The number of controls to add to gate (default=1).
label: An optional gate label.
ctrl_state: The control state in decimal or as
a bitstring (e.g. '111'). If specified as a bitstring the length
must equal num_ctrl_qubits, MSB on left. If None, use
2**num_ctrl_qubits-1.
Returns:
Controlled version of gate.
Raises:
CircuitError: gate contains non-gate in definition
"""
from math import pi
# pylint: disable=cyclic-import
from qiskit.circuit import controlledgate
ctrl_state = _ctrl_state_to_int(ctrl_state, num_ctrl_qubits)
q_control = QuantumRegister(num_ctrl_qubits, name="control")
q_target = QuantumRegister(operation.num_qubits, name="target")
q_ancillae = None # TODO: add Which causes the if mode is None:
# if enough ancillary qubits are provided, use the 'v-chain' method
additional_vchain = MCXGate.get_num_ancilla_qubits(len(control_qubits), "v-chain")
if len(ancillary_qubits) >= additional_vchain:
mode = "basic"
else:
mode = "noancilla" Will always choose "noancilla" given elif mode == "noancilla":
n_c = len(control_qubits)
if n_c == 1: # cu
_apply_cu(
self, theta, 0, 0, control_qubits[0], target_qubit, use_basis_gates=use_basis_gates
)
elif n_c < 4:
theta_step = theta * (1 / (2 ** (n_c - 1)))
_apply_mcu_graycode(
self,
theta_step,
0,
0,
control_qubits,
target_qubit,
use_basis_gates=use_basis_gates,
)
else:
if isinstance(theta, ParameterExpression):
raise QiskitError(f"Cannot synthesize MCRY with unbound parameter: {theta}.")
cgate = _mcsu2_real_diagonal(
RYGate(theta).to_matrix(),
num_controls=len(control_qubits),
use_basis_gates=use_basis_gates,
)
self.compose(cgate, control_qubits + [target_qubit], inplace=True) It will go to the |
Indeed, we currently do not have methods to transpile parameterized MCRY gates with unbound parameters, regardless of the number of ancillas, @Cryoris and @ShellyGarion please correct me if I am wrong. For instance, the function _mcsu2_real_diagonal, used in the code snippet above, requires all of the parameters to be floats and does not work with general parameter expressions. What you can do it to call assign_parameters prior to transpilation (this was recently added in #12869), e.g. the following code does work:
Also note that by default |
@Augusto12 does this resolve the problem in your workflow? 🙂 |
No. I'm using parameterized circuits in a machine learning workflow with the lib qiskit_machine_learning. When I put the inputs, the pipeline calls some transpilation tasks that break the flow. |
Could you post a minimally reproducing example of what you're doing? It might be something that can be resolved in the machine learning package itself 🙂 You could open an issue there directly or post it here and we might transfer it. |
Sure!
If you uncomment the line |
Ok I see, the problem here is that There's two possible resolutions to that
Neither of those things are handled in this repository, depending on which we would like to pursue, we should open an issue in |
Ok. Thank you very much. |
As a workaround you could use auxiliary qubits to implement the n-controlled, parameterized RY, for example something like from qiskit.circuit import Parameter
from qiskit.circuit.library import RYGate, MCMTVChain
y = Parameter("y")
mcry = MCMTVChain(RYGate(y), num_ctrl_qubits, 1) This circuit implements the controlled RY on the top |
This should be fixed now that #13507 is merged. If this is not so - please re-open this issue. |
Environment
What is happening?
I can't transpile a parameterized circuit with more than 3 controls
How can we reproduce the issue?
What should happen?
I got the error:
QiskitError: 'Cannot synthesize MCRY with unbound parameter: p[0].'
Any suggestions?
No response
The text was updated successfully, but these errors were encountered: