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] qml.taper is generating a tapered Hamiltonian with wires in the wrong order #6934

Open
1 task done
CatalinaAlbornoz opened this issue Feb 6, 2025 · 0 comments
Open
1 task done
Labels
bug 🐛 Something isn't working

Comments

@CatalinaAlbornoz
Copy link
Contributor

Expected behavior

qml.taper generates a tapered Hamiltonian with wires in the same order as the original Hamiltonian.

E.g. Suppose the original Hamiltonian has wires=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
If we taper off wires 7, 9, 10, and 11, I expect the wires of the tapered Hamiltonian to be wires=[0, 1, 2, 3, 4, 5, 6, 8]

Actual behavior

Instead we get wires=[1, 3, 5, 6, 8, 0, 2, 4]

Additional information

This was originally identified from Forum topic 7956. When this problem is fixed please mention it in the forum thread, linking to the PR that fixed the issue.

Source code

import pennylane as qml
from jax import numpy as jnp
#import jax
#jax.config.update('jax_enable_x64', True)
#jax.config.update("jax_platform_name", "cpu")

# Define the molecule
symbols = ['Li','H']
geometry = jnp.array([[0.0, 0.0, 0.0], [0.0, 0.0, 2.969280527]])
n_electrons = 4
charge = 0
molecule = qml.qchem.Molecule(symbols, geometry, charge=charge)

# Generate the Hamiltonian
H, qubits = qml.qchem.molecular_hamiltonian(molecule, mapping='jordan_wigner')

# Taper the Hamiltonian
generators = qml.symmetry_generators(H)
paulixops = qml.paulix_ops(generators, qubits)
paulix_sector = qml.qchem.optimal_sector(H, generators, n_electrons)
H_tapered = qml.taper(H, generators, paulixops, paulix_sector)
H_tapered_coeffs, H_tapered_ops = H_tapered.terms()
H_tapered = qml.Hamiltonian(jnp.real(jnp.array(H_tapered_coeffs)), H_tapered_ops)

# create the original and sparse tapered Hamiltonians
H_sparse = qml.SparseHamiltonian(H.sparse_matrix(), wires=H.wires)
H_tapered_sparse = qml.SparseHamiltonian(H_tapered.sparse_matrix(), wires=H_tapered.wires)

# Confirm that the smallest eigenvalue matches
print("Eigenvalues of H:\n", qml.eigvals(H_sparse, k=16))
print("\nEigenvalues of H_tapered:\n", qml.eigvals(H_tapered_sparse, k=4))

# Show that the wires for H_tapered_sparse are in the wrong order
print("H_sparse: ",H_sparse)
print("H_tapered_sparse: ",H_tapered_sparse)

# Calculate the HF energy without tapering
dev = qml.device("default.qubit", wires=H.wires)
@qml.qnode(dev, interface="jax")
def circuit():
    qml.BasisState(jnp.array([1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0]), wires=H.wires)
    return qml.state()

qubit_state = circuit()
HF_energy = qubit_state.T @ H.sparse_matrix().toarray() @ qubit_state
print(f"HF energy: {jnp.real(HF_energy):.8f} Ha")

# Calculate the HF energy with tapering and show that it's wrong unless we change the wire order in the BasisState
dev = qml.device("lightning.qubit", wires=H_tapered.wires)
@qml.qnode(dev, interface="jax")
def circuit():
    # The energy is correct when changing the wire ordering of the basis state to match the weird wire ordering [1, 1, 0, 0, 0, 1, 1, 0]
    qml.BasisState(jnp.array([1, 1, 1, 1, 0, 0, 0, 0]), wires=H_tapered.wires) 
    return qml.state()

qubit_state = circuit()
HF_energy = qubit_state.T @ H_tapered.sparse_matrix().toarray() @ qubit_state
print(f"HF energy (tapered): {jnp.real(HF_energy):.8f} Ha")

Tracebacks

System information

Name: PennyLane
Version: 0.40.0
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.11/dist-packages
Requires: appdirs, autograd, autoray, cachetools, diastatic-malt, networkx, numpy, packaging, pennylane-lightning, requests, rustworkx, scipy, tomlkit, typing-extensions
Required-by: PennyLane_Lightning

Platform info:           Linux-6.1.85+-x86_64-with-glibc2.35
Python version:          3.11.11
Numpy version:           1.26.4
Scipy version:           1.13.1
Installed devices:
- default.clifford (PennyLane-0.40.0)
- default.gaussian (PennyLane-0.40.0)
- default.mixed (PennyLane-0.40.0)
- default.qubit (PennyLane-0.40.0)
- default.qutrit (PennyLane-0.40.0)
- default.qutrit.mixed (PennyLane-0.40.0)
- default.tensor (PennyLane-0.40.0)
- null.qubit (PennyLane-0.40.0)
- reference.qubit (PennyLane-0.40.0)
- lightning.qubit (PennyLane_Lightning-0.40.0)

Existing GitHub issues

  • I have searched existing GitHub issues to make sure the issue does not already exist.
@CatalinaAlbornoz CatalinaAlbornoz added the bug 🐛 Something isn't working label Feb 6, 2025
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

1 participant