qutip / qutip-qip

The QuTiP quantum information processing package
https://qutip-qip.readthedocs.io/en/stable/
BSD 3-Clause "New" or "Revised" License
119 stars 63 forks source link

Add Variational Quantum Algorithms Module #118

Closed EnBr55 closed 2 years ago

EnBr55 commented 2 years ago

QuTiP currently lacks any functionality directly related to VQAs. It would be useful to have a class-based implementation that allows for problem specification down to the details of parametrized/fixed Hamiltonians and the optimisation method.

To outline a possible structure for this module, I have implemented something along these lines in my fork of this repository. This includes:

  1. Optimization_Result class
    • A convenient interface for results of the VQA optimisation method.
  2. VQA_Block class
    • A kind of pseudo-circuit-element. Holds one of either a Hamiltonian, a Unitary, or a pre-defined QuTiP gate (e.g. qutip_qip.operations.x_gate).
    • Takes a list of qubit targets
    • Can be either an "initial" element or not - this will determine if the block is repeated in the circuit layers.
    • In the case that a Hamiltonian or Unitary is specified, the action of the block is implemented by adding a new entry to the QubitCircuit's user_gates dictionary.
  3. VQA class
    • Initialised with n_qubits, n_layers
    • Can specify a cost function in terms of:
    • An observable
    • A state
    • A bitstring (single measurement outcome sampled from the circuit)
    • add_block(block: VQA_Block) appends VQA_Block to the circuit and, if necessary, updates the circuit's user_gates dictionary to include the new gate.
    • optimise_parameters method performs optimisation on the free parameters of the circuit and returns an Optimization_Result instance.
    • Methods that serve as a wrapper to QubitCircuit to simulate the circuit

A usage example can be found at https://github.com/EnBr55/qutip-vqa-examples/blob/main/notebooks/QAOA.ipynb.

I'm currently working on a Parameterized_Hamiltonian model as a VQA_Block input that will allow for defining more general quantum control problems.

Any discussion/suggestions as to these proposed models would be great!

nathanshammah commented 2 years ago

This is great @EnBr55, looking forward to discussing more with @BoxiLi and other contributors. As a small comment, all names proposed look very "pythonic" :) and the tutorial looks great.

BoxiLi commented 2 years ago

Hi @EnBr55, thanks very much for dragting this issue. This is very intriguing. Adding a VQA will definitely be a very useful feature to qutip-qip. It can also be easily integrated with the pulse-level simulation once this VQA class is constructed. Feel free to open a PR and happy to discuss!

Some thoughts of mine:

EnBr55 commented 2 years ago

Thanks for your thoughts, @BoxiLi !

BoxiLi commented 2 years ago

Thanks for the answers.

The VQA_Block I think is closer to what you mean by special type of gate

Yes I meant VQA_Block, somehow I forgot the "block" after I started typing :) These all make sense.

The "initial" elements are intended as a convenience for problem/circuit initialisation.

Ah I think now I get it, the initial layer and those repeated once.

About the Parameterized_Hamiltonian. If I understand correctly, each VQE block will be computed by

U = expm(H0 + c_1 * H_1 + c_2 * H_2 + ...)

where c_i are the parameters for VQA to optimize. What Processor does is implementing this

pairing free parameters with Hamiltonian terms

in a more sophisticated and modulated way. In the framework of Processor, those Hamiltonian Qobjs are saved in Model and the control coefficient c_i are generated by GateCompiler. In this sense, VQA with Hamiltonian based ansatz can even be implemented as a subclass of Processor that optimize the compilers! Of course, because VQA has quite some optimization procedures just for itself, it is probably better to define it as a separate class and just using Processor for the Hamiltonian ansatz or QubitCircuit for the usual circuit.

In the second case, I can think of a workflow like this

This might look like a detour. However, the advantage of building this on top of Processoris that you automatically get the functionality such as switching between expm and qutip solvers, using the noise model, using continuous pulse shape instead of piecewise constant, etc.

For small implementation and testing whether the Hamiotnian ansatz works at all, it is faster I guess to use expm and build the unitary gates within VQA_block. I'm just saying that when this pairing part starts to get too long and complicated, you might find some of these workflows already written in qutip-qip. You could have a look at the paper for detailed examples if you are interested.

EnBr55 commented 2 years ago

I like this workflow suggested with the Processor class. I'll see if I can implement this after the base stuff is done, thanks!

ajgpitch commented 2 years ago

Indeed, a very interesting project.

Sorry to be very dull, but should we not avoid underscores in class names?

EnBr55 commented 2 years ago

Good catch, thanks - not sure why my linter didn't pick that up.