pdhoolia / qiskit

Qiskit is an open-source SDK for working with quantum computers at the level of extended quantum circuits, operators, and primitives.
https://www.ibm.com/quantum/qiskit
Apache License 2.0
0 stars 0 forks source link

Help to transpile a parameterized circuit with more than 3 controls #10

Open pdhoolia opened 2 hours ago

pdhoolia commented 2 hours ago

I can't transpile a parameterized circuit with more than 3 controls

from qiskit.compiler import transpile
from qiskit.circuit import QuantumCircuit, ParameterVector

p = ParameterVector('p',4)

pqc = QuantumCircuit(4)
for i in range(4):
    pqc.ry(p[i],i)

pqc = pqc.control(4,0,annotated=True)
pqc = transpile(pqc, basis_gates=['u1', 'u2', 'u3', 'cx'], optimization_level=3)

With the code above I got the error: QiskitError: 'Cannot synthesize MCRY with unbound parameter: p[0].'

Is there a way I can work around this. Like call assign_parameters prior to transpilation?

pdhoolia commented 2 hours ago

The error you're encountering is due to the fact that Qiskit currently cannot synthesize multi-controlled rotation gates (like MCRY) when the parameters are unbound. This is because the synthesis of such gates requires concrete numerical values to generate the appropriate gate decomposition.

To work around this issue, you should assign the parameters before transpiling the circuit. This means using the assign_parameters method on your quantum circuit to bind the parameters to specific values before you attempt to transpile it. Here's how you can modify your code to do this:

  1. Assign parameters before calling transpile.
  2. Ensure that your circuit is fully parameterized with numerical values.

Here's the updated code:

from qiskit.compiler import transpile
from qiskit.circuit import QuantumCircuit, ParameterVector

# Define your parameters
p = ParameterVector('p', 4)

# Create a parameterized quantum circuit
pqc = QuantumCircuit(4)
for i in range(4):
    pqc.ry(p[i], i)

# Assign specific values to the parameters
param_values = {p[i]: 0.5 for i in range(4)}  # Example: binding all parameters to 0.5
pqc = pqc.assign_parameters(param_values)

# Control the circuit with 4 controls
pqc = pqc.control(4, 0, annotated=True)

# Transpile the circuit
pqc = transpile(pqc, basis_gates=['u1', 'u2', 'u3', 'cx'], optimization_level=3)

print(pqc)

Explanation:

Additional Insights:

This approach should resolve the error you're encountering and allow you to successfully transpile your circuit.