Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hamiltonian unitary #5

Open
rafaeleb opened this issue Aug 1, 2022 · 12 comments
Open

Hamiltonian unitary #5

rafaeleb opened this issue Aug 1, 2022 · 12 comments

Comments

@rafaeleb
Copy link

rafaeleb commented Aug 1, 2022

Hello all,

I was wondering if there is a way in TensorLy Quantum to build a parametrised unitary based on a binary Hamiltonian, such as the Ising model given in the examples, for use in the circuits.

I mean to use it in an application like a QAOA, for instance. Is there a way to adapt from the binary_hamiltonian function, or something like that?

Thanks!

@taylorpatti
Copy link
Collaborator

Hi rafaelb. To clarify your question, are you asking if there is a way to build a circuit unitary

U = exp(-itH)

where H is some Hamiltonian of interest (e.g., the Ising model Hamiltonian)?

@rafaeleb
Copy link
Author

rafaeleb commented Aug 3, 2022

Hi Taylor, thanks for the reply.

That's correct, where your t would not be time but some optimisable parameter.

I believe it could be done by breaking up the Hamiltonian into a bunch of other gates, but I am wondering if there is an easier way to go about it.

Rafael

@taylorpatti
Copy link
Collaborator

Hi Rafael,

Great, we do have such a gate class. It's called InvolutoryGeneratorUnitary. So the signature would be:

InvolutoryGeneratorUnitary(nqubits, ncontraq, involutory_generator)

where involutory_generator is a list of the different Pauli words in tensor form that you'd like to include. For instance, if the term that you want is ZZXZ then you would set

involutory_generator = [tlq.pauli_z(), tlq.pauli_z(), tlq.pauli_x(), tlq.pauli_z()

The one stipulation is that the terms indeed be involutory (their own inverse), but this will hold for many quantum operators used in such protocols (i.e., the Pauli strings).

@rafaeleb
Copy link
Author

rafaeleb commented Aug 9, 2022

Hi Taylor,

Thank you, I missed that class in the source files!

So I understand that, for the Ising Hamiltonian, it will create a ZZ term using something like

involutory_generator = [tlq.pauli_z(), tlq.pauli_z(), tlq.identity(), tlq.identity()]

where I am assuming a 4 qubit circuit. However, for each of these layers I will have an automatically generated parameter with the InvolutoryGeneratorUnitary class. Do you have any tip on how to get the full Hamiltonian (all ZZ terms) with the same optimisable parameter? Is there an obvious way of doing that?

@taylorpatti
Copy link
Collaborator

Hi Rafael,

Yep, you are correct, that is how you would write such a single ZZ term.

To verify that we are on the same page about your next question, what you would like to do is to write a four-qubit Hamiltonian H with all two-qubit Pauli strings in the z-basis and arbitrary coefficients such that

H = aZ0Z1 + bZ0Z2 + cZ0Z3 + dZ1Z2 + eZ1Z3 + fZ2Z3

and you would like to have a single optimization parameter gamma such that your unitary reads

U = exp(igammaH)

Is this correct?

@rafaeleb
Copy link
Author

Hi Taylor,

That's right! Ideally it would generalize it to more qubits of course, but in this simple example that is precisely the case. It seems I could get a single parameter gamma for many layers by modifying the InvolutoryGenerator class, but there might be a more natural way for it.

Appreciate the help!

Rafael

@taylorpatti
Copy link
Collaborator

Hi Rafael,

In that case, yes, exactly, you would want to use the same parameter across the different InvolutoryGeneratorUnitaries. The reason being that we cannot add the operators and keep them involutory (their own inverse) in general, as while Z0Z1 and Z2Z3 are themselves involutory, (Z0Z1+Z2Z3)(Z0Z1+Z2Z3) != I.

A way that you might prefer would be to do the Taylor Expansion up to some power of your choosing, then using tensor sum to add the terms. We have at least one TLQ user who is doing this.

Also, for small angled rotations, you can indeed push your entire H to InvolutoryGeneratorUnitary, as the error only comes in the 2nd order terms and above and if your time evolution is small enough you can neglect these.

@rafaeleb
Copy link
Author

Hi Taylor,

Thanks for the reply! Regarding this:

that case, yes, exactly, you would want to use the same parameter across the different InvolutoryGeneratorUnitaries

The best way to do this would be through adapting the InvolutoryGenerator class to deal with several layers of unitaries? That seems like the best option to me (shouldn't be too complex, I think), but any insight here is appreciated.

Best wishes,

Rafael

@taylorpatti
Copy link
Collaborator

Hi Rafael,

Yes, agreed, you could write a version of InvolutoryGenerator to apply several terms with the same parameter. If it's working well in your private repo and you write some test functions for it, I'd be happy to merge it to TensorLy-Quantum as well.

There may also be a method of directly feeding in the same parameter (not the data nor the clone but literally the same pointer) into the other InvolutoryGenerator instances...... this road pose some PyTorch issues though, definitely something to test.

@rafaeleb
Copy link
Author

Hi Taylor,

Thanks again for the tips! I will try to work out some solution for this and I will get back if I find something.

Rafael

@nadbp
Copy link

nadbp commented May 5, 2023

Hi,

I feel confused about the API. How do I implement a general QAOA using Tensorly-Quantum? I guess I need to use TTCircuit.forward_expectation_value, but the second parameter needs to be a (matrix) hamiltonian instead of a parameterized (problem) unitary. How should I use InvolutoryGeneratorUnitary? Could you please give me an example? Thank you!

@rafaeleb
Copy link
Author

rafaeleb commented Aug 2, 2023

Hi nadpb,

If your Hamiltonian can be written as a sum of involutory terms, you can adapt the InvolutoryGenerator and InvolutoryGeneratorUnitary classes to include an optimizable parameter, which is what I assume you want to do with QAOA.

I have added the following classes to the tt_gates.py module to implement my version of it, see below. Hope it helps!

par_inv_classes.txt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants