Qiskit / 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
5.1k stars 2.34k forks source link

Invalid Parameters with CU gate in AerSimulator #12625

Closed ProThicc closed 2 months ago

ProThicc commented 3 months ago

Environment

What is happening?

Custom Single Control Unitary gate shows Invalid Operation for AerSimulator from qiskit_aer

How can we reproduce the issue?

from qiskit import QuantumCircuit
from qiskit_aer import AerSimulator
from qiskit.circuit.add_control import add_control
from qiskit.circuit.library import UGate
qc = QuantumCircuit(3)
Sim = AerSimulator()
gate = UGate(-1.945632646294958, 1.5120405041931422, 2.4393357222229826)
qca_gate_reduced = add_control(gate, label='test', num_ctrl_qubits=1, ctrl_state=1)
qc.append(qca_gate_reduced, [1, 0])
qc.save_statevector()
result = Sim.run(qc).result().data(0)

What should happen?

Simulator should function giving the statevector after the Control Operation

Any suggestions?

Probably passing a default value of gamma for cu Gate should resolve the problem (I suspect that is the issue)

chrisamirani commented 3 months ago

@ProThicc instead of using add_control , have you tried using the internal control method? like this: qca_gate_reduced = gate.control(label='test', num_ctrl_qubits=1, ctrl_state=1)

ProThicc commented 3 months ago

@ProThicc instead of using add_control , have you tried using the internal control method? like this: qca_gate_reduced = gate.control(label='test', num_ctrl_qubits=1, ctrl_state=1)

When I try that I get this error, which makes sense that the CU gate defined when num_ctrls = 1 requires an additional parameter gamma. This same issue shows up with add_control which basically doesn't include this parameter when passing this gate as an instruction to the simulator. I have added a patch which by default sets this gamma variable as 0. This issue doesn't show up when num_ctrl_qubits >1 since the additional variables shown up are implicitly set to be 0

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[6], [line 8](vscode-notebook-cell:?execution_count=6&line=8)
      [6](vscode-notebook-cell:?execution_count=6&line=6) Sim = AerSimulator()
      [7](vscode-notebook-cell:?execution_count=6&line=7) gate = UGate(-1.945632646294958, 1.5120405041931422, 2.4393357222229826)
----> [8](vscode-notebook-cell:?execution_count=6&line=8) qca_gate_reduced = gate.control(label='test', num_ctrl_qubits=1, ctrl_state=1)
      [9](vscode-notebook-cell:?execution_count=6&line=9) # qc.append(qca_gate_reduced, [1, 0])
     [10](vscode-notebook-cell:?execution_count=6&line=10) # qc.cu(-1.945632646294958, 1.5120405041931422, 2.4393357222229826,control_qubit=1, target_qubit=0, gamma=0, label='test')
     [11](vscode-notebook-cell:?execution_count=6&line=11) qc.save_statevector()

File ~/anaconda3/envs/qca/lib/python3.11/site-packages/qiskit/circuit/library/standard_gates/u.py:121, in UGate.control(self, num_ctrl_qubits, label, ctrl_state, annotated)
    [107](https://file+.vscode-resource.vscode-cdn.net/home/pratik/codes/QCA_open_system/sandbox_notebooks/QIP_sandbox/~/anaconda3/envs/qca/lib/python3.11/site-packages/qiskit/circuit/library/standard_gates/u.py:107) """Return a (multi-)controlled-U gate.
    [108](https://file+.vscode-resource.vscode-cdn.net/home/pratik/codes/QCA_open_system/sandbox_notebooks/QIP_sandbox/~/anaconda3/envs/qca/lib/python3.11/site-packages/qiskit/circuit/library/standard_gates/u.py:108) 
    [109](https://file+.vscode-resource.vscode-cdn.net/home/pratik/codes/QCA_open_system/sandbox_notebooks/QIP_sandbox/~/anaconda3/envs/qca/lib/python3.11/site-packages/qiskit/circuit/library/standard_gates/u.py:109) Args:
   (...)
    [118](https://file+.vscode-resource.vscode-cdn.net/home/pratik/codes/QCA_open_system/sandbox_notebooks/QIP_sandbox/~/anaconda3/envs/qca/lib/python3.11/site-packages/qiskit/circuit/library/standard_gates/u.py:118)     ControlledGate: controlled version of this gate.
    [119](https://file+.vscode-resource.vscode-cdn.net/home/pratik/codes/QCA_open_system/sandbox_notebooks/QIP_sandbox/~/anaconda3/envs/qca/lib/python3.11/site-packages/qiskit/circuit/library/standard_gates/u.py:119) """
    [120](https://file+.vscode-resource.vscode-cdn.net/home/pratik/codes/QCA_open_system/sandbox_notebooks/QIP_sandbox/~/anaconda3/envs/qca/lib/python3.11/site-packages/qiskit/circuit/library/standard_gates/u.py:120) if not annotated and num_ctrl_qubits == 1:
--> [121](https://file+.vscode-resource.vscode-cdn.net/home/pratik/codes/QCA_open_system/sandbox_notebooks/QIP_sandbox/~/anaconda3/envs/qca/lib/python3.11/site-packages/qiskit/circuit/library/standard_gates/u.py:121)     gate = CUGate(
    [122](https://file+.vscode-resource.vscode-cdn.net/home/pratik/codes/QCA_open_system/sandbox_notebooks/QIP_sandbox/~/anaconda3/envs/qca/lib/python3.11/site-packages/qiskit/circuit/library/standard_gates/u.py:122)         self.params[0],
    [123](https://file+.vscode-resource.vscode-cdn.net/home/pratik/codes/QCA_open_system/sandbox_notebooks/QIP_sandbox/~/anaconda3/envs/qca/lib/python3.11/site-packages/qiskit/circuit/library/standard_gates/u.py:123)         self.params[1],
    [124](https://file+.vscode-resource.vscode-cdn.net/home/pratik/codes/QCA_open_system/sandbox_notebooks/QIP_sandbox/~/anaconda3/envs/qca/lib/python3.11/site-packages/qiskit/circuit/library/standard_gates/u.py:124)         self.params[2],
    [125](https://file+.vscode-resource.vscode-cdn.net/home/pratik/codes/QCA_open_system/sandbox_notebooks/QIP_sandbox/~/anaconda3/envs/qca/lib/python3.11/site-packages/qiskit/circuit/library/standard_gates/u.py:125)         label=label,
    [126](https://file+.vscode-resource.vscode-cdn.net/home/pratik/codes/QCA_open_system/sandbox_notebooks/QIP_sandbox/~/anaconda3/envs/qca/lib/python3.11/site-packages/qiskit/circuit/library/standard_gates/u.py:126)         ctrl_state=ctrl_state,
    [127](https://file+.vscode-resource.vscode-cdn.net/home/pratik/codes/QCA_open_system/sandbox_notebooks/QIP_sandbox/~/anaconda3/envs/qca/lib/python3.11/site-packages/qiskit/circuit/library/standard_gates/u.py:127)     )
    [128](https://file+.vscode-resource.vscode-cdn.net/home/pratik/codes/QCA_open_system/sandbox_notebooks/QIP_sandbox/~/anaconda3/envs/qca/lib/python3.11/site-packages/qiskit/circuit/library/standard_gates/u.py:128)     gate.base_gate.label = self.label
    [129](https://file+.vscode-resource.vscode-cdn.net/home/pratik/codes/QCA_open_system/sandbox_notebooks/QIP_sandbox/~/anaconda3/envs/qca/lib/python3.11/site-packages/qiskit/circuit/library/standard_gates/u.py:129) else:

TypeError: CUGate.__init__() missing 1 required positional argument: 'gamma'
chrisamirani commented 3 months ago

@ProThicc I highly suspect you have an old version. If you look at this line , you'll see the default 0 for gamma.

ProThicc commented 3 months ago

@ProThicc I highly suspect you have an old version. If you look at this line , you'll see the default 0 for gamma.

import qiskit qiskit.version.get_version_info()

'1.1.0'

Also the same line of the code is present in my version of qiskit. The issue arises in add_control

chrisamirani commented 3 months ago

@ProThicc hmmm interesting. Well if you look at the error you sent above you can see the invocation of the method I referenced in the error:

  [120](https://file+.vscode-resource.vscode-cdn.net/home/pratik/codes/QCA_open_system/sandbox_notebooks/QIP_sandbox/~/anaconda3/envs/qca/lib/python3.11/site-packages/qiskit/circuit/library/standard_gates/u.py:120) if not annotated and num_ctrl_qubits == 1:
--> [121](https://file+.vscode-resource.vscode-cdn.net/home/pratik/codes/QCA_open_system/sandbox_notebooks/QIP_sandbox/~/anaconda3/envs/qca/lib/python3.11/site-packages/qiskit/circuit/library/standard_gates/u.py:121)     gate = CUGate(
    [122](https://file+.vscode-resource.vscode-cdn.net/home/pratik/codes/QCA_open_system/sandbox_notebooks/QIP_sandbox/~/anaconda3/envs/qca/lib/python3.11/site-packages/qiskit/circuit/library/standard_gates/u.py:122)         self.params[0],
    [123](https://file+.vscode-resource.vscode-cdn.net/home/pratik/codes/QCA_open_system/sandbox_notebooks/QIP_sandbox/~/anaconda3/envs/qca/lib/python3.11/site-packages/qiskit/circuit/library/standard_gates/u.py:123)         self.params[1],
    [124](https://file+.vscode-resource.vscode-cdn.net/home/pratik/codes/QCA_open_system/sandbox_notebooks/QIP_sandbox/~/anaconda3/envs/qca/lib/python3.11/site-packages/qiskit/circuit/library/standard_gates/u.py:124)         self.params[2],
    [125](https://file+.vscode-resource.vscode-cdn.net/home/pratik/codes/QCA_open_system/sandbox_notebooks/QIP_sandbox/~/anaconda3/envs/qca/lib/python3.11/site-packages/qiskit/circuit/library/standard_gates/u.py:125)         label=label,
    [126](https://file+.vscode-resource.vscode-cdn.net/home/pratik/codes/QCA_open_system/sandbox_notebooks/QIP_sandbox/~/anaconda3/envs/qca/lib/python3.11/site-packages/qiskit/circuit/library/standard_gates/u.py:126)         ctrl_state=ctrl_state,
    [127](https://file+.vscode-resource.vscode-cdn.net/home/pratik/codes/QCA_open_system/sandbox_notebooks/QIP_sandbox/~/anaconda3/envs/qca/lib/python3.11/site-packages/qiskit/circuit/library/standard_gates/u.py:127)     )
    [128](https://file+.vscode-resource.vscode-cdn.net/home/pratik/codes/QCA_open_system/sandbox_notebooks/QIP_sandbox/~/anaconda3/envs/qca/lib/python3.11/site-packages/qiskit/circuit/library/standard_gates/u.py:128)     gate.base_gate.label = self.label
    [129](https://file+.vscode-resource.vscode-cdn.net/home/pratik/codes/QCA_open_system/sandbox_notebooks/QIP_sandbox/~/anaconda3/envs/qca/lib/python3.11/site-packages/qiskit/circuit/library/standard_gates/u.py:129) else:

Which tells me whatever python environment you run your script on has a version of qiskit that does not have the default gamma 0. But as i mentioned on this line we clearly see there's a default gamma. So something weird must be going on on your python setup.

As for the code I suggested, I was able to run and got the following output.

{'statevector': Statevector([1.+0.j, 0.+0.j, 0.+0.j, 0.-0.j, 0.+0.j, 0.+0.j, 0.+0.j,
              0.-0.j],
             dims=(2, 2, 2))}

I'd say make sure you don't have a conflicting qiskit version somewhere on your system by clearing all your python qiskit installations and start fresh to see if it resolves your issue.

ProThicc commented 3 months ago

@ProThicc hmmm interesting. Well if you look at the error you sent above you can see the invocation of the method I referenced in the error:

  [120](https://file+.vscode-resource.vscode-cdn.net/home/pratik/codes/QCA_open_system/sandbox_notebooks/QIP_sandbox/~/anaconda3/envs/qca/lib/python3.11/site-packages/qiskit/circuit/library/standard_gates/u.py:120) if not annotated and num_ctrl_qubits == 1:
--> [121](https://file+.vscode-resource.vscode-cdn.net/home/pratik/codes/QCA_open_system/sandbox_notebooks/QIP_sandbox/~/anaconda3/envs/qca/lib/python3.11/site-packages/qiskit/circuit/library/standard_gates/u.py:121)     gate = CUGate(
    [122](https://file+.vscode-resource.vscode-cdn.net/home/pratik/codes/QCA_open_system/sandbox_notebooks/QIP_sandbox/~/anaconda3/envs/qca/lib/python3.11/site-packages/qiskit/circuit/library/standard_gates/u.py:122)         self.params[0],
    [123](https://file+.vscode-resource.vscode-cdn.net/home/pratik/codes/QCA_open_system/sandbox_notebooks/QIP_sandbox/~/anaconda3/envs/qca/lib/python3.11/site-packages/qiskit/circuit/library/standard_gates/u.py:123)         self.params[1],
    [124](https://file+.vscode-resource.vscode-cdn.net/home/pratik/codes/QCA_open_system/sandbox_notebooks/QIP_sandbox/~/anaconda3/envs/qca/lib/python3.11/site-packages/qiskit/circuit/library/standard_gates/u.py:124)         self.params[2],
    [125](https://file+.vscode-resource.vscode-cdn.net/home/pratik/codes/QCA_open_system/sandbox_notebooks/QIP_sandbox/~/anaconda3/envs/qca/lib/python3.11/site-packages/qiskit/circuit/library/standard_gates/u.py:125)         label=label,
    [126](https://file+.vscode-resource.vscode-cdn.net/home/pratik/codes/QCA_open_system/sandbox_notebooks/QIP_sandbox/~/anaconda3/envs/qca/lib/python3.11/site-packages/qiskit/circuit/library/standard_gates/u.py:126)         ctrl_state=ctrl_state,
    [127](https://file+.vscode-resource.vscode-cdn.net/home/pratik/codes/QCA_open_system/sandbox_notebooks/QIP_sandbox/~/anaconda3/envs/qca/lib/python3.11/site-packages/qiskit/circuit/library/standard_gates/u.py:127)     )
    [128](https://file+.vscode-resource.vscode-cdn.net/home/pratik/codes/QCA_open_system/sandbox_notebooks/QIP_sandbox/~/anaconda3/envs/qca/lib/python3.11/site-packages/qiskit/circuit/library/standard_gates/u.py:128)     gate.base_gate.label = self.label
    [129](https://file+.vscode-resource.vscode-cdn.net/home/pratik/codes/QCA_open_system/sandbox_notebooks/QIP_sandbox/~/anaconda3/envs/qca/lib/python3.11/site-packages/qiskit/circuit/library/standard_gates/u.py:129) else:

Which tells me whatever python environment you run your script on has a version of qiskit that does not have the default gamma 0. But as i mentioned on this line we clearly see there's a default gamma. So something weird must be going on on your python setup.

As for the code I suggested, I was able to run and got the following output.

{'statevector': Statevector([1.+0.j, 0.+0.j, 0.+0.j, 0.-0.j, 0.+0.j, 0.+0.j, 0.+0.j,
              0.-0.j],
             dims=(2, 2, 2))}

I'd say make sure you don't have a conflicting qiskit version somewhere on your system by clearing all your python qiskit installations and start fresh to see if it resolves your issue.

Gotcha, I just fresh installed qiskit and it updated this part of the module to include default parameters. Thanks!

1ucian0 commented 2 months ago

I dont thinkqiskit.circuit.add_control.add_control is not part of the public API (maybe I didnt search correctly) so I think this issue can be closed.

Thanks @chrisamirani for this great explanation!