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

Wired input and output of function "c.amplitude" #147

Open
FelixXu35 opened this issue May 12, 2023 · 3 comments
Open

Wired input and output of function "c.amplitude" #147

FelixXu35 opened this issue May 12, 2023 · 3 comments
Labels
bug Something isn't working

Comments

@FelixXu35
Copy link
Contributor

Issue Description

For a quantum circuit with n qubits, function "c.amplitude" returns the amplitude of a given quantum state. It will work when an n-bit binary string is passed into. An error will raise if the length of the string is not n, except n+1. If an (n+1)-bit string is passed into, "c.amplitude" will return the amplitude of a state corresponding to the first n bits, while the last bit controls the "phase". For example, if a circuit has 3 qubits, and one amplitude is
"000" -> 0.5
then
"0000" -> 0.5
"0001" -> 0.5*j
and j is the imaginary unit

How to Reproduce

Screenshot 2023-05-12 at 23 14 19

Error Output

Screenshot 2023-05-12 at 23 14 39

Environment Context

macOS 13.3.1(a)
tensorcircuit 0.8.0
BTW, why tc.about() gives me "module 'tensorcircuit' has no attribute 'about'"?

@FelixXu35 FelixXu35 added the bug Something isn't working label May 12, 2023
@refraction-ray
Copy link
Contributor

BTW, why tc.about() gives me "module 'tensorcircuit' has no attribute 'about'"?

you can upgrade you tensorcircuit version: pip install -U tensorcircuit.

Thanks for report the n+1 qubit input case for c.amplitude, can be reproduced, will have a look

@refraction-ray
Copy link
Contributor

turns out it is a feature related to tensornetwork, when there is only one disentangling edge (one extra bit string), the contractor doesn't raise error since unspecified output edge is unique.

See code below:

for n in [2, 3, 4]:
    ns = []
    for _ in range(n):
        ns.append(tc.gates.Gate(np.array([1.0, 0.0])))
    
    ns[0][0]^ns[1][0]
    try:
        print(n, tc.contractor(ns).tensor)
    except ValueError:
        print(n, "ValueError")

The output will be:

2 1.0
3 [1. 0.]
4 ValueError

If you really want to fix it (nothing really wrong if the use specify the correct bitstring), adding a length check at the beginning of amplitude method is enough. Welcome to PR on this error check protection

@FelixXu35
Copy link
Contributor Author

Thanks for your reply.

I agree. Nothing is really wrong.

But I believe it is worthy to add a length check, because the error message of a (<n) binary string is:

"list index out of range"

and of a (>n+1) binary string is:

"The final node after contraction has more than one remaining edge. In this case output_edge_order has to be provided."

Neither of them is clear for a beginner.

I will give a try.

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

2 participants