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] Hamiltonians defined using operator arithmetic queuing extra gates #5454

Closed
1 task done
alvaro-at-xanadu opened this issue Apr 1, 2024 · 0 comments · Fixed by #5455
Closed
1 task done

[BUG] Hamiltonians defined using operator arithmetic queuing extra gates #5454

alvaro-at-xanadu opened this issue Apr 1, 2024 · 0 comments · Fixed by #5455
Labels
bug 🐛 Something isn't working

Comments

@alvaro-at-xanadu
Copy link

Expected behavior

When defining

dev = qml.device('default.qubit', wires = [0,1])

def H(alpha, beta):
  coeffs = [alpha, beta]
  ops = [qml.PauliX(0) @ qml.PauliX(1),qml.PauliZ(0) @ qml.PauliZ(1)]
  return qml.dot(coeffs,ops)

def H2(alpha, beta):
  return alpha * qml.PauliX(0) @ qml.PauliX(1) + beta * qml.PauliZ(0) @ qml.PauliZ(1)

@qml.qnode(dev)
def Ham_evolve(alpha, beta, time):

    qml.evolve(H(alpha,beta), coeff = time)

    return qml.probs(wires = [0,1])
  
@qml.qnode(dev)
def Ham_evolve2(alpha, beta, time):

    qml.evolve(H2(alpha,beta), coeff = time)

    return qml.probs(wires = [0,1])

I would expect Ham_evolve and Ham_evolve2 to build the same circuit.

Actual behavior

Drawing print(qml.draw(Ham_evolve, expansion_strategy="device")(0.9,1.0,0.4)) gives the correct circuit, since the Hamiltonian was defined with qml.dot.

image

Drawing print(qml.draw(Ham_evolve2, expansion_strategy="device")(0.9,1.0,0.4)) queues extra gates.

image

This can be fixed by running qml.operation.enable_new_opmath() but it does seem like an extra step users wouldn't be aware of.

Additional information

Happens all the time with version 0.35.1, as long as new_opmath is disabled.

Source code

Working examples are above.

Tracebacks

No error tracebacks since it's a queuing issue.

System information

Name: PennyLane
Version: 0.35.1
Summary: PennyLane is a cross-platform Python library for quantum computing, quantum machine learning, and quantum chemistry. Train a quantum computer the same way as a neural network.
Home-page: https://github.com/PennyLaneAI/pennylane
Author: 
Author-email: 
License: Apache License 2.0
Location: /usr/local/lib/python3.10/dist-packages
Requires: appdirs, autograd, autoray, cachetools, networkx, numpy, pennylane-lightning, requests, rustworkx, scipy, semantic-version, toml, typing-extensions
Required-by: PennyLane_Lightning

Platform info:           Linux-6.1.58+-x86_64-with-glibc2.35
Python version:          3.10.12
Numpy version:           1.25.2
Scipy version:           1.11.4
Installed devices:
- lightning.qubit (PennyLane_Lightning-0.35.1)
- default.clifford (PennyLane-0.35.1)
- default.gaussian (PennyLane-0.35.1)
- default.mixed (PennyLane-0.35.1)
- default.qubit (PennyLane-0.35.1)
- default.qubit.autograd (PennyLane-0.35.1)
- default.qubit.jax (PennyLane-0.35.1)
- default.qubit.legacy (PennyLane-0.35.1)
- default.qubit.tf (PennyLane-0.35.1)
- default.qubit.torch (PennyLane-0.35.1)
- default.qutrit (PennyLane-0.35.1)
- null.qubit (PennyLane-0.35.1)

Existing GitHub issues

  • I have searched existing GitHub issues to make sure the issue does not already exist.
@alvaro-at-xanadu alvaro-at-xanadu added the bug 🐛 Something isn't working label Apr 1, 2024
@alvaro-at-xanadu alvaro-at-xanadu changed the title Hamiltonians defined using operator arithmetic queuing extra gates [BUG] [BUG] Hamiltonians defined using operator arithmetic queuing extra gates Apr 1, 2024
albi3ro added a commit that referenced this issue Apr 1, 2024
**Context:**

A user was encountering issues where operations were remaining in the
circuit after being used to construct a hamiltonian.

**Description of the Change:**

De-queue the arguments to `Hamiltonian.__matmul__`

**Benefits:**

No more things in the circuit that shouldn't be in the circuit.

**Possible Drawbacks:**

**Related GitHub Issues:**

Fixes #5454 [sc-60158]
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.

1 participant