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

Unable to Create Multi-Controlled Gates with More Than 3 Controls in Qiskit #12801

Closed
Augusto12 opened this issue Jul 22, 2024 · 11 comments
Closed
Labels
bug Something isn't working

Comments

@Augusto12
Copy link

Environment

  • Qiskit version: 1.0.2
  • Python version: 3.10
  • Operating system: Ubuntu 22.04

What is happening?

I am encountering an issue where I cannot create multi-controlled gates with more than 3 controls in Qiskit.

How can we reproduce the issue?

Code Example:

from qiskit import QuantumCircuit
from qiskit.circuit import Parameter

theta1 = Parameter('θ1')

qc = QuantumCircuit(5)
qc.ry(theta1,4)
qc.control(4,ctrl_state=0)

qc.draw("mpl")

What should happen?

I got the error: TypeError: ParameterExpression with unbound parameters (dict_keys([Parameter(θ1)])) cannot be cast to a float.

Any suggestions?

No response

@Augusto12 Augusto12 added the bug Something isn't working label Jul 22, 2024
@ShellyGarion
Copy link
Member

Thanks for finding this bug. This issue should be solved after #12752 will be merged.

@ShellyGarion
Copy link
Member

closing this issue since #12752 was merged

@Augusto12
Copy link
Author

Were you able to run the code from this issue after the merge? @ShellyGarion

@ShellyGarion
Copy link
Member

@Augusto12 - you should slightly update your code with the new parameter annotated as follows:

        from qiskit import QuantumCircuit
        from qiskit.circuit import Parameter

        theta1 = Parameter('θ1')

        qc = QuantumCircuit(5)
        qc.ry(theta1, 4)
        qc.control(4, ctrl_state=0, annotated=True)
        qc.draw() 

gives the circuit:

q_0: ──────────
               
q_1: ──────────
               
q_2: ──────────
               
q_3: ──────────
     ┌────────┐
q_4: ┤ Ry(θ1) ├
     └────────┘

@Cryoris
Copy link
Contributor

Cryoris commented Jul 30, 2024

@ShellyGarion's solution works, alternatively you could create the RYGate directly which doesn't require you arguing about the annotation. This is also a cleaner solution as it doesn't create an intermediate gate called "ry" which is then controlled later on.

from qiskit.circuit import QuantumCircuit, Parameter
from qiskit.circuit.library import RYGate

theta1 = Parameter('θ1')
mcry = RYGate(theta1).control(4, ctrl_state=0)

qc = QuantumCircuit(5)
qc.append(mcry, range(5))
qc.draw()

giving

q_0: ────o─────
         │
q_1: ────o─────
         │
q_2: ────o─────
         │
q_3: ────o─────
     ┌───┴────┐
q_4: ┤ Ry(θ1) ├
     └────────┘

@Augusto12
Copy link
Author

Thanks!

@Augusto12
Copy link
Author

Hi again.

With this approach, I can no longer access the parameters.

The code:

qc.parameters

Always returns ParameterView([])

@ShellyGarion
Copy link
Member

This code:

        from qiskit import QuantumCircuit
        from qiskit.circuit import Parameter

        theta1 = Parameter('θ1')

        qc = QuantumCircuit(5)
        qc.ry(theta1, 4)
        qc.control(4, ctrl_state=0, annotated=True)
        qc.parameters 

outputs:
ParameterView([Parameter(θ1)])

@Augusto12
Copy link
Author

Ok. @ShellyGarion , could you share your qc.data? When you shared qc.draw(), the printed circuit was without controls. I'm confused.

@ShellyGarion
Copy link
Member

ShellyGarion commented Aug 4, 2024

@Augusto12 - you should slightly update your code as follows (note that in your original code you have printed the circuit qc which has only ry, and not the updated controlled circuit). Please make sure that you have the updated version of Qiskit.

        from qiskit import QuantumCircuit
        from qiskit.circuit import Parameter

        theta1 = Parameter('θ1')

        qc = QuantumCircuit(5, name="my_ry")
        qc.ry(theta1, 4)
        qc1 = qc.control(4, ctrl_state=0, annotated=True)
        print (qc1.parameters)
        print (qc1.data)
        print (qc1)

outputs:

ParameterView([Parameter(θ1)])
[CircuitInstruction(operation=<qiskit.circuit.annotated_operation.AnnotatedOperation object at 0x7f8a5e94c380>, qubits=(Qubit(QuantumRegister(4, 'q0'), 0), Qubit(QuantumRegister(4, 'q0'), 1), Qubit(QuantumRegister(4, 'q0'), 2), Qubit(QuantumRegister(4, 'q0'), 3), Qubit(QuantumRegister(5, 'q'), 0), Qubit(QuantumRegister(5, 'q'), 1), Qubit(QuantumRegister(5, 'q'), 2), Qubit(QuantumRegister(5, 'q'), 3), Qubit(QuantumRegister(5, 'q'), 4)), clbits=())]
                    
q0_0: ──────o───────
            │       
q0_1: ──────o───────
            │       
q0_2: ──────o───────
            │       
q0_3: ──────o───────
      ┌─────┴──────┐
 q_0: ┤0           ├
      │            │
 q_1: ┤1           ├
      │            │
 q_2: ┤2 My_ry(θ1) ├
      │            │
 q_3: ┤3           ├
      │            │
 q_4: ┤4           ├
      └────────────┘

@ShellyGarion
Copy link
Member

ShellyGarion commented Aug 4, 2024

In addition, the code that @Cryoris suggested above produces the following output (with the latest version of Qiskit).

        from qiskit.circuit import QuantumCircuit, Parameter
        from qiskit.circuit.library import RYGate

        theta1 = Parameter('θ1')
        mcry = RYGate(theta1).control(4, ctrl_state=0)

        qc = QuantumCircuit(5)
        qc.append(mcry, range(5))
        print (qc.parameters)
        print (qc.data)
        print (qc)

outputs:

ParameterView([Parameter(θ1)])
[CircuitInstruction(operation=<qiskit.circuit.annotated_operation.AnnotatedOperation object at 0x7f8a5e859430>, qubits=(Qubit(QuantumRegister(5, 'q'), 0), Qubit(QuantumRegister(5, 'q'), 1), Qubit(QuantumRegister(5, 'q'), 2), Qubit(QuantumRegister(5, 'q'), 3), Qubit(QuantumRegister(5, 'q'), 4)), clbits=())]
               
q_0: ────o─────
         │     
q_1: ────o─────
         │     
q_2: ────o─────
         │     
q_3: ────o─────
     ┌───┴────┐
q_4: ┤ Ry(θ1) ├
     └────────┘

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

3 participants