PennyLane
Next

To attempt this challenge, please switch to a larger screen size.

Beginner
Quantum Chemistry

Hi, Hydrogen!

Challenge statement

This challenge is deprecated and will not work with the current version of PennyLane. If you would like to attempt it, feel free to paste the code into a local environment using a PennyLane version prior or equal to v0.36.

The Variational Quantum Eigensolver (VQE) algorithm has been touted as a game-changing near-term quantum algorithm. In particular, VQE is able to efficiently simulate low-energy properties of small molecules. In this challenge, you will calculate the energy of the hydrogen molecule for various molecular charges and bond length combinations.

Challenge code

In the code below, you are given a few functions:

  • hydrogen_hamiltonian: This function will return the qubit Hamiltonian of the hydrogen molecule, H_2, given the coordinates of both hydrogen atoms and the net molecular charge. You'll usually find H_2 with a charge of 0, but here we'll spice it up with a non-zero charge!
  • num_electrons: In subsequent functions, we'll need the total number of electrons in the hydrogen molecule we're looking at. With a charge of 0, H_2 usually has just 2 electrons, one per hydrogen atom. Given the charge, how many electrons should H_2 have? You must complete this function.
  • hf: The "HF" stands for Hartree–Fock. This function's purpose is calculate the HF approximation — treat every electron as independent, electrons move under a Coulomb potential from the positively charged nuclei, and there's a mean field from the other electrons — for the ground state of the hydrogen molecule we're interested in. We'll use this later, so you must complete this function.
  • run_VQE: This function takes the coordinates, charge, generates the HF state, defines a cost function and minimizes it. You must complete this function by:
    • defining the gates within the cost function, using the qml.AllSinglesDoubles template with singles and doubles arguments defined below; and
    • returning what we want to minimize, namely the expectation value of the hydrogen Hamiltonian!

Here are some helpful resources:

  • Building molecular Hamiltonians
  • A brief overview of VQE
  • Variational Quantum Eigensolver
  • Quantum Chemistry documentation

Input

As input to this problem, you are given:

  • coordinates (list(float)): the x, y, and z coordinates of each hydrogen atom
  • charge (int): the charge of the hydrogen molecule. It could be positive, negative, or zero!

Output

This code must output the ground state energy (float) of the hydrogen molecule in question.

Test cases

The following public test cases are available to you. Note that there are additional hidden test cases that we use to verify that your code is valid in full generality.

test_input: [[0.0, 0.0, -0.8, 0.0, 0.0, 0.8], -1] expected_output: -0.53168359 test_input: [[0.0, 0.0, -0.6614, 0.0, 0.0, 0.6614], 0] expected_output: -1.03104296

If your solution matches the correct one within the given tolerance specified in check (in this case it's a 1e-3 relative error tolerance), the output will be "Success!". Otherwise, you will receive an "Incorrect" prompt.

Good luck!

Loading...