Using lists of strings in qml.Hamiltonian to get Hamiltonian!

Hello! I am working with qml.Hamiltonian. I’m using a function to find all the common elements between two different Hamiltonians and then trying to convert this back to get a Hamiltonian but I am getting an error (provided below). I understand this might be the case because they are strings now and no longer pennylane objects however I’ve tried to used obs.compare() function that pennylane provides and I’m not getting the results I’d hoped for. So just wondering if there are any fixes to this?

H2_data = qml.data.load("qchem", molname = "H2", basis = "STO-3G", bondlength = 1.1)[0]
H3_data = qml.data.load("qchem", molname = "H3+", basis = "STO-3G", bondlength = 1.1)[0] 

def find_common_elements(list1, list2):
    a = []
    b = []
    
    common_elements = []

    aa = []
    bb = []
    for i in list1:
        a.append(str(i))
    for j in list2:
        b.append(str(j))
    
    for one in a:
        for two in b:
            if one == two:
                common_elements.append(one)
    
    for item in a:
        if item not in common_elements:
            aa.append(item)
    
    for item1 in b:
        if item1 not in common_elements:
            bb.append(item1)
    return common_elements, aa, bb

common_result = find_common_elements(H2_data.hamiltonian.ops, H3_data.hamiltonian.ops)
commons = common_result[0]
new_Hamiltonian = qml.Hamiltonian(H2_hamiltonian.coeffs, commons)
print(new_Hamiltonian)

This is the error I am getting:

Traceback (most recent call last):
  File "c:\Users\sfrod\Documents\Quantum Ethics Project\Algorithm-Research\Student-Hub\Serene-Rodrigues\molecules.py", line 85, in <module>
    newH2_hamiltonian = qml.Hamiltonian(H2_hamiltonian.coeffs, commons)
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\sfrod\AppData\Local\Programs\Python\Python311\Lib\site-packages\pennylane\ops\qubit\hamiltonian.py", line 187, in __init__
    raise ValueError(
ValueError: Could not create circuits. Some or all observables are not valid.

Here is my qml.about():
Name: PennyLane
Version: 0.31.1
Summary: PennyLane is a Python quantum machine learning library by Xanadu Inc.
Home-page: GitHub - PennyLaneAI/pennylane: PennyLane is a cross-platform Python library for differentiable programming of quantum computers. Train a quantum computer the same way as a neural network.
Author:
Author-email:
License: Apache License 2.0
Location: C:\Users\sfrod\AppData\Local\Programs\Python\Python311\Lib\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: Windows-10-10.0.22621-SP0
Python version: 3.11.1
Numpy version: 1.23.5
Scipy version: 1.10.1
Installed devices:

  • default.gaussian (PennyLane-0.31.1)
  • default.mixed (PennyLane-0.31.1)
  • default.qubit (PennyLane-0.31.1)
  • default.qubit.autograd (PennyLane-0.31.1)
  • default.qubit.jax (PennyLane-0.31.1)
  • default.qubit.tf (PennyLane-0.31.1)
  • default.qubit.torch (PennyLane-0.31.1)
  • default.qutrit (PennyLane-0.31.1)
  • null.qubit (PennyLane-0.31.1)
  • lightning.qubit (PennyLane-Lightning-0.31.0)
    None

Hey @Serene_Rodrigues , welcome to the forum! :smiley:

I think we’re missing the definition of H2_hamiltonian in your code, do you mind quickly including it so we have a fully self-contained piece of code? That would help us figure out the exact problem. :slight_smile:

Hi sorry about that, that can be replaced with H2_data.hamiltonian.coeffs

Hi @Serene_Rodrigues ,

The issue is indeed the fact that you’re converting everything into a string.

You can compare the different observables if you use them as they are. For example the code below will return True.

H2_data.hamiltonian.ops[0].compare(H2_data.hamiltonian.ops[0])

Note that here I’m using the format ob1.compare(ob2)

You can then modify your code in order to use the observables themselves instead of the string for them.

I hope this helps!

1 Like