I completed all 5 exercises in the IBM Quantum Challenge 2021 — here’s how I did it! Part 2
By: Luke Johnson
Tags: IBM, Quantum Computing, Hackathon, Quantum, Qiskit

You can now see above my pulses that were sent to the qubit. First of all, the far left blue pulse which is at the frequency of the |0> to |1> transition, and then the second spectroscopy pulse which varies from this frequency to find the |1> to |2> transition frequency. The red pulse is then the measurement pulse which is very important as I saw when it was missed out. You can see the syntax of the code that got us there.

All that was left to do was modify fitting parameters moderately to make sure my drive frequency fitted the results well and that was Exercise 4 complete. With the Intermediate badge now in the bag it was time to have a break, recharge some batteries and dive into Exercise 5 in the quest for Advanced status…

Exercise 5 (the final challenge): Variational Quantum Eigensolver (VQE)

I was finally onto the 5th and final challenge, one step away from the coveted Advanced digital badge. The topic for the final challenge was the Variational Quantum Eigensolver, a hybrid classic-quantum algorithm (using each part for its strength), first used to find the ground state energy of a molecule. In 2017, the IBM Quantum team first used the VQE algorithm to simulate the ground state energy of the Lithium Hydride molecule. The task was to use the newly released Qiskit Nature to simulate just that, the ground state energy of Lithium Hydride. The whole premise behind using quantum computers for potential use cases like this one, is that molecules such as Lithium Hydride are fundamentally quantum, they abide by quantum mechanical theory.

Qiskit Nature is a new platform within Qiskit released in April 2021 which aims to bridge the gap between natural science and quantum simulations.

Before this challenge, I had some experience using the VQE in the financial services domain. So I knew the basics, that we’d have to construct a classical problem, that was converted into a quantum operator (problem), with an Ansatz (approximation of our ground state, i.e. a guess). This would then be fed into the VQE algorithm along with a classical optimiser. The VQE would then perform all of the magic under the Qiskit Nature covers, and return us the minimum eigenvalue (solution), whether that be the ground state energy in this example, or maybe the optimal solution in another realm/application.

Workflow representing VQE simulations for molecules

The notebook gives a great introduction to these concepts and also a link to the video tutorial on the Qiskit YouTube channel. It also gives a description of the molecular Hamiltonian, which helps us to understand how the quantum operators are formed but isn’t imperative to solve the exercise. The tutorial then takes us through the same method for another molecule H². It encourages us to look at the attributes of the qmolecule that is defined, this can be found in the Qiskit Nature documentation, and provides us with information about the molecule that we can use to help us in the problem.

Qmolecule documentation from Qiskit Nature

The example then uses the ElectronicStructureProblem function again from Qiskit Nature, to define our problem and map the fermionic operators of the molecule to qubits. It then converts this to a Hamiltonian by returning the second-quantised operators (an attribute from the above ElectronicStructureProblem) and taking the 0th index from it.

The next step is to define the mapping we want to use and convert the fermionic operators to qubit operators. In this example, the Jordan Wigner Mapper is used to map a single qubit to a single spin orbital in the molecule. This is where I started to become worried… I’d understood the QM behind the Hamiltonians and operators but my lack of Chemistry knowledge when presented with these mappers was starting to appear in my mind. This is the beauty of Qiskit Nature, it allows you to simply follow a prescribed pathway through a method without necessarily knowing every detail about Chemistry or even quantum computing. This continued into the next step setting our initial state to the Hartree Fock state, which I was promised in the notebook was a good initial state in chemistry…

The different types of Ansatz are then presented to us, UCCSD,PUCCD,SUCCD and TwoLocal. We will be using TwoLocal for this example. This circuit is also printed for us and we can see for this molecule we are using just 4 qubits.

Remember this is the guess that we are making for our ground state, so we can start as close as we can to the actual answer.

In the same vein, the classical optimisers we mentioned in the build up to the problem (that Qiskit provides) are then presented to us.

The notebook then works out using an exact classical method, the exact electronic energy for us to use as a benchmark for our future quantum method. Now this might confuse you… why would be use an approximate quantum method if we can just use a classical computer to work out the exact ground state? Well, the issue arises when we try to compute molecules of large sizes, which tend to be the ones we want to know most about. You would find we’d very quickly run out of memory.

Exact electronic energy for H²

We can now put all of our tools together to solve this problem using our VQE classical-quantum method by simply importing VQE from qiskit.algorithms and inputting our Ansatz, our optimiser, our backend and our initial point. We can then see the function runs an iterative process which converges to our ground state energy for H².

VQE set up

We have some success!!! This energy is the same as the exact energy as the exact Eigensolver gave us.

Phew, we’re now ready for the final challenge!

Again, in my usual summary method I like to try and give an overall picture of our problem. So we’re now tasked with performing the same method of solving the ground state energy, this time for LiH (a more complex molecule than H²) using the best ansatz we can. We are then given some clues on how to reduce the size of our circuit as the final score and our IBM Quantum challenge ranking will be based on this.

My first thought, was to simply construct the same method by copying down the code from above for all steps, and this time use the newly defined molecule as the input to the first step of our problem. At this point ignoring all of the hints as to how we could reduce the size of our circuit, I just tried to get a method whatever the score that worked.

This was relatively simple, just copying down all the code from the first method. Using the default Ansatz = TwoLocal, Optimiser = Coblya, and the JordanWignerMapper give you a circuit using 12 qubits. You can see how slow this combination of inputs works which is due to the size of the circuit and the number of qubits (The depth and width of the circuit). It also doesn’t converge to the classically computed benchmark of the correct answer.

First answer check…

We can see this circuit calculates the ground state energy to be -8.44163 which is far from the target that you can see in the image above. At this point I started to play around altering the Ansatz type to UCCSD but began to get errors that the depth of the circuit was exceeded. This told me that the UCCSD Ansatz may be more accurate, but at this point we are using too many qubits ie our problem is too complex for it to work. I messed around with the other types ‘PUCCD’ and the circuit does print, but again I can see the depth of the circuit is massive. This time the VQE does converge closer to the correct answer but uses 896 CNots !!!!!

So, after a quick play with the different types of Ansatz I was stumped. Then I remembered the hints that the notebook gave you to simplify your problem, to use less qubits and a less complex (costly) Ansatz, but still converge to the correct ground state energy.

This is the important part of the challenge. I didn’t think EX5 would be as simple as just copying and pasting code… So we are given three methods that we should investigate to reduce the number of qubits for our simulation. Let’s start with the first, freezing the core electrons that don’t significantly contribute to the problem (compared to the valence electrons). We’re told that this is an inbuilt functionality within qiskit. Let’s go to qiskit_nature.transformers to inspect the freeze core transformer.

Qiskit Nature documentation.

This tells us that the FreezeCoreTranformer is within the Qmolecule transformers (as the name suggests). After first of all importing it from the location in Qiskit shown above, we simply need to call q_molecule_transformers = FreezeCoreTransformer(freeze_cores=True) within the ElectronicStructureProblem function to cause our transformer to act on our Qmolecule and do just that, freeze the core orbitals.

FreezeCoreTransformer code

When we then go through the process of running the VQE, we notice that our Ansatz only has 10 qubits rather than the 12 that we had before. This is because 2 of the orbitals have been frozen. This reduces the cost of our circuit and with the PUCCD ansatz gets us very close to a correct answer(remember here to change freeze_core to True in the answer checker)

PCCD with Frozen core

The next method to reduce the complexity of our problem is to use Parity Mapper as our converter, and to enable two_qubit_reduction. The details of this can be found in the QubitConverter documentation in Qiskit Nature. We can see that a parameter of the Qubit converter is two_qubit_reduction and we simply need to set that as true just as before. You can see how I did this below;

Parity Mapper + 2 qubit reduction code

This parameter further reduces the number of qubits in the ansatz by you guessed it! Two. So we now have just 8 qubits, down from our original 12, which represent a ‘guess’ to our ground state energy problem. But again, this doesn’t quite get us a valid answer using the PUCCD, Two Local, or SUCCD optimisers. With either the answer not converging to the correct energy, or the circuit being too large. PUCCD being dangerously close…

PUCCD, two qubit reduction, + freeze cores.

Let’s move onto the third and final hint for the problem; reduce the number of qubits by inspecting the symmetries of our Hamiltonian and find a way to use Z2Symmetries in Qiskit. I remembered Z2Symmetries being a part of the converter when we looked at the two qubit reduction. So going back to that page to check out the function, I noticed it was another parameter within the QubitConverter called z2symmetry_reduction = [] and we had to enter a list of integers for which the resulting qubit operators could be split into two and require one less qubit for operation. So we need to find symmetries in our Hamiltonian that we can list here. I started by trying to insert guesses into the symmetry reduction, and fortunately the error message I received told me that I should enter tapering values with values of -1 or 1 with list length 2… I kept going until I found the value for -1’s and 1's that got me closest to the correct answer using our trusty PUCCD ansatz via trial and error. Also noticing our number of qubits was down to 6, half of what we started with. The UCCSD Ansatz at this point was still throwing up problems with the circuit being too large.

Going to the slack channel to see if other people were having this same issue, I noticed that some people were mentioning removing more orbitals to further reduce the size of our circuit. I saw in the FreezeCoreTransformer that there was also an option to remove_orbitals.

The orbital index I placed in here would be removed without any checks… In order to decide which orbital(s) I wanted to remove, I had to go back to the structure of the problem, and specifically LiH. It turns out the molecular configuration of LiH is 1s² 2s² , meaning we only need to consider these two orbitals, not the third (2p)… don’t worry if you didn’t study Quantum science and this means nothing to you. The indices of the configuration simply tell us the number of electrons in that orbital and 1s2s2p are the names of our first three atomic orbitals (this is described in further details here). With this knowledge that the 3rd orbital wasn’t contributing largely to the LiH, I added into the FreezeCoreTransformer the input to remove_orbitals=[3] to remove the third orbital and then had to change the length of the z2_symmetry_reduction input to length 1… I went with -1 as a guess.

I now noticed we were only using 5 qubits for our Ansatz. And finally, after removing orbital 3, freezing the core orbitals, enabling two qubit reduction, using parity mapper, with the SUCCD ansatz and using Cobyla. (Phew that was a long sentence) I had a correct answer (score of 198), and my hunt for the advanced badge was complete!!!!

Congratulations in order…

In time, I also noticed you can remove orbital 4 also for a score of 120… but that’s enough for this blog. Some brilliant scores and their solutions will be added to the Qiskit community GitHub here, which will show many more methods with much much lower scores and custom Ansatz’s.

I managed to get this far, but unfortunately this was after the challenge had finished…as it would have put me in ~16th place otherwise…

My final best score.

Thank you very much for following me on my IBM Quantum Challenge 2021 journey, I hope some of what I said was helpful for as many people as possible. This year’s challenge was a great opportunity to learn and further our Qiskit skills together as a community. Over 1400 participants submitted at least one answer which is a great testament to the challenge as a whole.

One big final thanks to the organisers of the challenge!!! My quest for the Advanced badge is complete!!!

See you all, same place, next time!

The views expressed in this blog are my own and do not necessarily reflect the views of IBM.