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

Update #556

merged 1 commit into from Feb 24, 2020

Update #556

merged 1 commit into from Feb 24, 2020


Copy link

josh146 commented Feb 22, 2020

Added PennyLane to the list of libraries that use OpenFermion.

The pennylane.qchem module of PennyLane allows fermionic Hamiltonians defined in OpenFermion to be used to create PennyLane variational circuits. These circuits support autodifferentiation (via TensorFlow or PyTorch) for optimization.

Please feel free to suggest better links --- as OpenFermion is integrated into the core PennyLane package (as opposed to importing a separate plugin), I wasn't sure the best resource to link to!


import tensorflow as tf
import numpy as np

from openfermion import FermionOperator, jordan_wigner
import pennylane as qml

H = FermionOperator("0^ 0", 0.5) + FermionOperator("1^ 1", 0.5) + FermionOperator("0", 2)
H = jordan_wigner(H)

# convert the OpenFermion Hamiltonian into PennyLane
# observables, and a list of coefficients
coeffs, obs = qml.qchem.convert_hamiltonian(H).terms

def ansatz(params, **kwargs):
    """Ansatz to be used in VQE algorithm"""
    qml.BasisState(np.array([1, 1, 0, 0]), wires=[0, 1, 2, 3])
    for i in range(4):
        qml.Rot(*params[i], wires=i)
    qml.CNOT(wires=[2, 3])
    qml.CNOT(wires=[2, 0])
    qml.CNOT(wires=[3, 1])

# define a quantum device for VQE algorithm evaluation
dev = qml.device("default.qubit", wires=4)

# Create the QNodes by mapping the ansatz over the observable list
# Each QNode calculates the expectation value <H_i> for each term
# in the molecular Hamiltonian.
qnodes =, obs, dev, interface="tf")

# the VQE cost function is defined as \sum_i c_i <H_i>
cost =, qnodes)

params = tf.Variable(tf.random.normal([4, 3], dtype=tf.float64))

# Perform the optimization using TensorFlow's SGD optimizer
opt = tf.keras.optimizers.SGD(learning_rate=0.1)
steps = 200

for i in range(steps):

    with tf.GradientTape() as tape:
        loss = cost(params)

    print("Step {}: cost = {}".format(i, loss))

    grad = tape.gradient(loss, [params])
    opt.apply_gradients(zip(grad, [params]))

print("Final cost:", cost(params))
@googlebot googlebot added the cla: yes label Feb 22, 2020
@josh146 josh146 requested a review from babbush Feb 22, 2020
@babbush babbush merged commit 1a9cb12 into quantumlib:master Feb 24, 2020
3 checks passed
3 checks passed
cla/google All necessary CLAs are signed
continuous-integration/travis-ci/pr The Travis CI build passed
coverage/coveralls Coverage increased (+0.01%) to 99.64%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

3 participants
You can’t perform that action at this time.