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

QNodes with default.qubit and default.qubit.legacy give different results #4779

Closed
1 task done
isaacdevlugt opened this issue Nov 2, 2023 · 2 comments · Fixed by #4781
Closed
1 task done

QNodes with default.qubit and default.qubit.legacy give different results #4779

isaacdevlugt opened this issue Nov 2, 2023 · 2 comments · Fixed by #4781
Labels
bug 🐛 Something isn't working

Comments

@isaacdevlugt
Copy link
Contributor

Expected behavior

They should give the same results.

Actual behavior

They give different results.

Additional information

Seems to be that BasisState followed by some (? — RandomLayers and some instances of QubitUnitary ) operation give different results.

Source code

import pennylane as qml
import pennylane.numpy as np

new_device = qml.device("default.qubit", wires=2)
legacy_device = qml.device("default.qubit.legacy", wires=2)

def random_unitary():
    weights = np.array([[0.1, -2.1, 1.4]])
    qml.RandomLayers(weights=weights, wires=0)

def circuit(state):
    qml.BasisState(state, wires=1) 
    random_unitary()
    # getting rid of BasisState and/or random_unitary produces no discrepancy

@qml.qnode(new_device)
def circuit_new_device(state):
    circuit(state)
    return qml.state()

@qml.qnode(legacy_device)
def circuit_legacy_device(state):
    circuit(state)
    return qml.state()

res_new_device = circuit_new_device(np.array([1]))
res_legacy_device = circuit_legacy_device(np.array([1]))

print(res_new_device == res_legacy_device) # [False False False False]

Tracebacks

N/A

System information

Name: PennyLane
Version: 0.33.0
Summary: PennyLane is a Python quantum machine learning library by Xanadu Inc.
Home-page: https://github.com/PennyLaneAI/pennylane
Author:
Author-email:
License: Apache License 2.0
Location: /Users/isaac/.virtualenvs/pennylane-stable/lib/python3.9/site-packages
Requires: appdirs, autograd, autoray, cachetools, networkx, numpy, pennylane-lightning, requests, rustworkx, scipy, semantic-version, toml, typing-extensions
Required-by: PennyLane-Lightning

Platform info:           macOS-14.1-x86_64-i386-64bit
Python version:          3.9.14
Numpy version:           1.23.5
Scipy version:           1.10.0
Installed devices:
- default.gaussian (PennyLane-0.34.0.dev0)
- default.mixed (PennyLane-0.34.0.dev0)
- default.qubit (PennyLane-0.34.0.dev0)
- default.qubit.autograd (PennyLane-0.34.0.dev0)
- default.qubit.jax (PennyLane-0.34.0.dev0)
- default.qubit.legacy (PennyLane-0.34.0.dev0)
- default.qubit.tf (PennyLane-0.34.0.dev0)
- default.qubit.torch (PennyLane-0.34.0.dev0)
- default.qutrit (PennyLane-0.34.0.dev0)
- null.qubit (PennyLane-0.34.0.dev0)
- lightning.qubit (PennyLane-Lightning-0.33.1)

Existing GitHub issues

  • I have searched existing GitHub issues to make sure the issue does not already exist.
@isaacdevlugt
Copy link
Contributor Author

Just checked that my code example produces the correct results with #4781 🚀

@trbromley
Copy link
Contributor

Thanks @isaacdevlugt! This is a great thing to spot, and something that would be great to be tested for in the tests. Thanks @albi3ro for fixing quickly.

albi3ro added a commit that referenced this issue Nov 3, 2023
Fixes #4779 . [sc-49381]

When creating an initial state, `get_final_state` was using
`tape.op_wires` for the wire order, even though the simulation always
occurred using the wire order `(0,1,2,3,...)`. Sometimes `tape.op_wires`
would have a different order, leading to a mismatch.

Now we use `sorted(circuit.op_wires)`, so they will always be in the
monotonically increasing integers used by the simulation.

---------

Co-authored-by: Matthew Silverman <[email protected]>
Co-authored-by: Tom Bromley <[email protected]>
timmysilv added a commit that referenced this issue Nov 8, 2023
Fixes #4779 . [sc-49381]

When creating an initial state, `get_final_state` was using
`tape.op_wires` for the wire order, even though the simulation always
occurred using the wire order `(0,1,2,3,...)`. Sometimes `tape.op_wires`
would have a different order, leading to a mismatch.

Now we use `sorted(circuit.op_wires)`, so they will always be in the
monotonically increasing integers used by the simulation.

---------

Co-authored-by: Matthew Silverman <[email protected]>
Co-authored-by: Tom Bromley <[email protected]>
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

Successfully merging a pull request may close this issue.

2 participants