# Variational Quantum Linear solver complexity issue

I am currently trying to implement the VQLS

for a 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.

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 Pennylane library.

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

Complete details
Attached is Ansatz am using and I am getting local cost function using optimizer (GradientDescentOptimizer) around 0.10 and if I increase more layers cost function starts 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 3 qubit matrix into multiplication or sequence of unitary matrices (ex Pauli).
for example (3 qubits) U = Pauli X1.Pauli Y2.Pauli X0

Hey @Nithin and welcome to the forum !

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?

This is the age-old question in machine learning! The answer to each of these parts depends on the problem you’re trying to solve. There’s no one-size-fits-all answer! A lot of it comes down to trial and error, which is usually the fastest path to success for me. Then I might analyze why that particular combination of things worked to begin with.

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.

If I understand correctly, you need to implement a circuit that does this:

(U_1 + U_2 + U_3)\vert \psi \rangle.

If that’s the case, check out our codebook: Xanadu Quantum Codebook. There’s a whole module on the theory and application of linear combination of unitaries! You might also find this video helpful: Optimizing Linear Combination of Unitaries for Quantum Chemistry Hamiltonians - YouTube

Regarding which variational circuit to choose

Looking at the ansatz you posted on stack exchange, it might be a good idea to have gates other than just R_y gates. Having said that, that’s not a guaranteed answer that will give you better results. I recommend:

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

We sure do! You can use qml.pauli_decompose. Here is an example where we decompose the Hadamard gate into a linear combination of Pauli gates:

U = 1 / np.sqrt(2) * np.array([[1, 1], [1, -1]])
print(qml.pauli_decompose(U))
'''
(0.7071067811865475) [X0]
+ (0.7071067811865475) [Z0]
'''


Hope this helps!