# Getting High cost function in code implementation of VQLS pennylane tutorial

I am currently trying to implement the tutorial in pennylane

https://pennylane.ai/qml/demos/tutorial_vqls.html

for very complex example in 3 Qubit and cost function is very high in spite of adding multiple layers in Variational circuit. I have two questions.

1. Can you suggest the best way to choose variational Ansatz and do you think I should consider different optimizers or any other suggestion to reduce cost function?

2. My matrix which converts zero state to output b state is not a quantum native matrix and I need to decompose it so how can we implement a linear combination of matrices in penny lane as an addition? Naturally, if we add gates it will be multiplication in circuit.

Thank you in advance and your reply would help me a lot in my understanding and implementing my code

Edit 1

I am trying to solve the linear equation Ax = b and The algorithm am using is VQLS and am trying to implement it in google collab using penny lane library.

A matrix and its decompostion and output b state am considering 3 qubit hadamard gate as of now

Attached is Ansatz am using and am getting local cost function using optimizer (GradientDescentOptimizer) around 0.10 and if I increase more layers cost function started increasing so I need suggestions

1. Regarding which variational circuit to choose

2. (Not relevant to my problem) Do we have any existing function or code which decomposes any matrix into multiplication or sequence of unitary matrices (ex Pauli).
for example (3 qubit) U = Pauli X1.Pauli Y2.Pauli X0

• In order for anyone to provide you a good answer, you will have to add a few details to your question. For example: what is the linear system you are trying to solve? which ansatz are you using? which optimiser? which cost function (the global one? local one?)? Also, $b$ is not a matrix in linear systems or VQLS, could you precise your second point? Commented Apr 21, 2023 at 9:11
• Thanks for the feedback..I have added more information in the above edit Commented Apr 22, 2023 at 13:49

There are several points that might be the cause of your issue, I will try to make a synthetic answer.

First, about which variational circuit to choose. This is a whole research subject and is 100% problem-dependent. The goal of the variational circuit is to approximate the solution to your problem, and you would like to be able to know if a given circuit will be able to approximate the solution without knowing it beforehand.

In the specific case of VQLS, there is no "specialised" ansatz like the UCCSD ansatz for VQE for example. This is due to the fact that the solution of a linear system of equation can be any vector in the space. So in general, you cannot have any specialised ansatz for VQLS.

Now, for your particular linear system, you know that your solution will only contain real numbers, with an imaginary part of $$0$$. This is an information you can use (and you already use in your example by only using Ry rotations) to reduce the number of parameters in the ansatz.

In terms of optimisers, this is also a whole research subject. In the end, the cost function obtained with most (all?) variational algorithms is very hard to optimise due to:

• Barren plateaus
• The abundance of local minimas

In particular, the fact that the cost function has a lot of local minimas means that it is difficult to get the global minimum, i.e., the solution to your problem. I think that the increase of local minimas is the reason why your cost is not lowering anymore: the optimiser is stuck in a local minima and cannot escape from it. This is not something solvable easily. In practice, global optimisation is a known hard problem when your problem does not have sufficient structure (for example, convexity), and variational algorithm cost functions are notoriously hard for the existing optimisers (lot of local minimas to be stuck in).

TLDR: yes it is a hard problem, no I do not see obvious ways to improve the cost function in your particular example, yes the problems listed above apply to all quantum variational algorithms I know of, are not solved yet, and are a big deal for the efficiency and correctness of quantum variational methods.