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

Bug in U2 gate parameter initialization #232

Open
yezhuoyang opened this issue Feb 10, 2024 · 2 comments
Open

Bug in U2 gate parameter initialization #232

yezhuoyang opened this issue Feb 10, 2024 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@yezhuoyang
Copy link
Collaborator

This is the code I use that has some bugs related with parameter initialization(In dev branch):

import torchquantum as tq
import torchquantum.functional as tqf

qdev = tq.QuantumDevice(n_wires=2, bsz=5, device="cpu", record_op=True)  # use device='cuda' for GPU

# use tqf
tq.u2(qdev, [0], [0.1, 0.2])

# print the current state (dynamic computation graph supported)
print(qdev)

U2 gate is the rotation gate with two parameters
U2 gate in qiskit

Running the above example will generate the following error message:

image

The error is caused by some strange manipulation on the dimension of the tensor, I don't understand why we have to use some squeeze and unsqueeze here.

image

It seems that many other gates with more than one parameters have similar issue. We should set the standard of how should user pass the parameters in. And perhaps more code comments in the above part can help developer to understand the convention of tensor dimensions.

@yezhuoyang yezhuoyang added the bug Something isn't working label Feb 10, 2024
@Hanrui-Wang
Copy link
Collaborator

This is because we should use tq.u2(qdev, [0],[[0.1, 0.2]]) for the computation with bsz>1

Let's fix it to make sure it runs with [0.1, 0.2] also

@tring27
Copy link

tring27 commented Mar 12, 2024

I think it should be ?
if params.dim() == 1:
params = params.unsqueeze(0).unsqueeze(-1)

tring27 added a commit to tring27/torchquantum that referenced this issue Mar 12, 2024
Bug in U2 gate parameter initialization mit-han-lab#232
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