# Catalyst for QAOA

**URL:** <https://discuss.pennylane.ai/t/catalyst-for-qaoa/5300>\
**Category:** Catalyst\
**Created:** [September 30, 2024, 5:54pm UTC](https://discuss.pennylane.ai/t/catalyst-for-qaoa/5300 "2024-09-30T17:54:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bach\_Bao](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/bach_bao/32/2135_2.png) [@Bach\_Bao](https://discuss.pennylane.ai/u/Bach_Bao)\
**Post date:** [September 30, 2024, 5:54pm UTC](https://discuss.pennylane.ai/t/catalyst-for-qaoa/5300/1 "2024-09-30T17:54:25Z")

</div>

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:

```auto
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:

```auto
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,

```auto
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:

 ![image](https://canada1.discourse-cdn.com/flex012/uploads/pennylane/original/2X/6/6c19852ec8030dc649ee34e60317a9e9a90a8c66.png)  
 ![image](https://canada1.discourse-cdn.com/flex012/uploads/pennylane/original/2X/a/acaf0fe39a9259ca05858f5a0d199a9881535ab3.png)

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

Thanks a lot.

---

<div class="post-metadata">

**Author:** ![CatalinaAlbornoz](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/catalinaalbornoz/32/1196_2.png) [@CatalinaAlbornoz](https://discuss.pennylane.ai/u/CatalinaAlbornoz)\
**Post date:** [October 1, 2024, 12:04am UTC](https://discuss.pennylane.ai/t/catalyst-for-qaoa/5300/2 "2024-10-01T00:04:04Z")

</div>

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](https://docs.pennylane.ai/projects/catalyst/en/stable/dev/quick_start.html)) 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.

---

<div class="post-metadata">

**Author:** ![David\_Ittah](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/david_ittah/32/2395_2.png) [@David\_Ittah](https://discuss.pennylane.ai/u/David_Ittah)\
**Post date:** [October 1, 2024, 7:44pm UTC](https://discuss.pennylane.ai/t/catalyst-for-qaoa/5300/3 "2024-10-01T19:44:41Z")

</div>

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 🙂

Let me know if you run into any more trouble!

---

<div class="post-metadata">

**Author:** ![Bach\_Bao](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/bach_bao/32/2135_2.png) [@Bach\_Bao](https://discuss.pennylane.ai/u/Bach_Bao)\
**Post date:** [October 1, 2024, 11:36pm UTC](https://discuss.pennylane.ai/t/catalyst-for-qaoa/5300/4 "2024-10-01T23:36:08Z")

</div>

Hi @David_Ittah and @CatalinaAlbornoz,

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

```auto
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

 ![image](https://canada1.discourse-cdn.com/flex012/uploads/pennylane/original/2X/1/12d6de58725f6b33412e8d0b299f3731e64c34e3.png)

---

<div class="post-metadata">

**Author:** ![David\_Ittah](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/david_ittah/32/2395_2.png) [@David\_Ittah](https://discuss.pennylane.ai/u/David_Ittah)\
**Post date:** [October 2, 2024, 2:17pm UTC](https://discuss.pennylane.ai/t/catalyst-for-qaoa/5300/5 "2024-10-02T14:17:57Z")

</div>

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`
