Catalyst for QAOA

Hi there,
It has been a while since my last touch with Pennylane and Catalyst, and now I want to use Catalyst as a way to speed up QAOA optimization but I am running into some issues that I hope to get help with…
My pennylane version:

Name: PennyLane
Version: 0.36.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: /work/acslab/users/baobach/anaconda3/envs/pennylane_catalyst/lib/python3.11/site-packages
Requires: appdirs, autograd, autoray, cachetools, networkx, numpy, pennylane-lightning, requests, rustworkx, scipy, semantic-version, toml, typing-extensions
Required-by: PennyLane-Catalyst, PennyLane_Lightning

Platform info:           Linux-3.10.0-957.27.2.el7.x86_64-x86_64-with-glibc2.17
Python version:          3.11.7
Numpy version:           1.26.4
Scipy version:           1.12.0
Installed devices:
- lightning.qubit (PennyLane_Lightning-0.36.0)
- default.clifford (PennyLane-0.36.0)
- default.gaussian (PennyLane-0.36.0)
- default.mixed (PennyLane-0.36.0)
- default.qubit (PennyLane-0.36.0)
- default.qubit.autograd (PennyLane-0.36.0)
- default.qubit.jax (PennyLane-0.36.0)
- default.qubit.legacy (PennyLane-0.36.0)
- default.qubit.tf (PennyLane-0.36.0)
- default.qubit.torch (PennyLane-0.36.0)
- default.qutrit (PennyLane-0.36.0)
- default.qutrit.mixed (PennyLane-0.36.0)
- null.qubit (PennyLane-0.36.0)
- nvidia.custatevec (PennyLane-Catalyst-0.6.0)
- nvidia.cutensornet (PennyLane-Catalyst-0.6.0)
- oqc.cloud (PennyLane-Catalyst-0.6.0)
- softwareq.qpp (PennyLane-Catalyst-0.6.0)

The implementation is as follows:

dev = qml.device("lightning.qubit", wires=n_wires, shots=1)
# unitary operator U_B with parameter beta
def U_B(beta):
    for wire in range(n_wires):
        qml.RX(2*beta, wires=wire) 

# unitary operator U_C with parameter gamma
def U_C(gamma):
    edges_set = list(G.edges)
    for edge in range(len(edges_set)):
        wire1 = edges_set[edge][0]
        wire2 = edges_set[edge][1]
        qml.CNOT(wires=[wire1, wire2])
        qml.RZ(gamma, wires=wire2)
        qml.CNOT(wires=[wire1, wire2])
# QAOA circuit
@qml.qnode(dev)
def circuit(gammas, betas, edge):
    qml.broadcast(qml.Hadamard, range(n_wires), pattern="single")
    for i in range(num_layers):
        U_C(gammas[i])
        U_B(betas[i])
    H = qml.PauliZ(edge[0]) @ qml.PauliZ(edge[1])
    return qml.expval(H) 
# minimize the negative of the objective function
@qml.qjit
def objective(params):
    gammas = params[0]
    betas = params[1]
    neg_obj = 0
    for edge in G.edges():
        # objective for the MaxCut problem
        neg_obj -= 0.5 * (1 - circuit(gammas, betas, edge=edge))
    return neg_obj     

When I try to execute the following code,

gammas = jnp.array(np.random.uniform(0, 1, size = (num_layers,)))
betas = jnp.array(np.random.uniform(0, 1, size = (num_layers,)))
init_params = jnp.array([betas, gammas])
objective(init_params)

There is an error as follows:


The error is kinda weird and hope someone can help me with this.

Thanks a lot.

Hi @Bach_Bao !

I see that you’re not using the latest PennyLane version. Could you please upgrade to use PennyLane v0.38.1 and Catalyst v0.8 and let us know if that fixes your error?

Also, could you please post a minimal reproducible example? Something that we can copy-paste to try to reproduce your issue.

In that minimal example I recommend that you start with a simple circuit decorated with @qjit (as shown in the examples in the documentation quick start) and then little by little add complexity until you reach an error.

I would also recommend that you give a thorough look at that quick start since I see that your code includes a lot of components which can cause issues such as for loops and more.

I hope this can help you.

Hi @Bach_Bao, I tried reproducing your issue but on my device with the latest version of Catalyst and PennyLane I cannot see any errors. Note that n_wires, num_layers, and G are missing from your sample code so I made some assumptions, but hopefully the issue will resolve for you as well once you update your packages :slight_smile:

Let me know if you run into any more trouble!

Hi @David_Ittah and @CatalinaAlbornoz,

Thanks for the support, I have already tried to update Pennylane to the latest version with the following:

Name: PennyLane
Version: 0.38.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: /work/acslab/users/baobach/anaconda3/envs/Pennylane_0.38/lib/python3.11/site-packages
Requires: appdirs, autograd, autoray, cachetools, networkx, numpy, packaging, pennylane-lightning, requests, rustworkx, scipy, toml, typing-extensions
Required-by: PennyLane-Catalyst, PennyLane_Lightning

Platform info:           Linux-3.10.0-957.27.2.el7.x86_64-x86_64-with-glibc2.17
Python version:          3.11.0
Numpy version:           1.26.4
Scipy version:           1.14.1
Installed devices:
- nvidia.custatevec (PennyLane-Catalyst-0.6.0)
- nvidia.cutensornet (PennyLane-Catalyst-0.6.0)
- oqc.cloud (PennyLane-Catalyst-0.6.0)
- softwareq.qpp (PennyLane-Catalyst-0.6.0)
- lightning.qubit (PennyLane_Lightning-0.38.0)
- default.clifford (PennyLane-0.38.0)
- default.gaussian (PennyLane-0.38.0)
- default.mixed (PennyLane-0.38.0)
- default.qubit (PennyLane-0.38.0)
- default.qubit.autograd (PennyLane-0.38.0)
- default.qubit.jax (PennyLane-0.38.0)
- default.qubit.legacy (PennyLane-0.38.0)
- default.qubit.tf (PennyLane-0.38.0)
- default.qubit.torch (PennyLane-0.38.0)
- default.qutrit (PennyLane-0.38.0)
- default.qutrit.mixed (PennyLane-0.38.0)
- default.tensor (PennyLane-0.38.0)
- null.qubit (PennyLane-0.38.0)

However, this does not seem to fix the problem, I am now facing this one

Hi @Bach_Bao, from your log it looks like you are still using catalyst version 0.6.0, the latest version is 0.8.1. Please make sure to update both PennyLand and Catalyst, you can use the following command for this:
pip install -U pennylane pennylane-lightning pennylane-catalyst