Skip to content

Commit

Permalink
Merge pull request #88 from qiboteam/dev_basisrotation
Browse files Browse the repository at this point in the history
Basis rotation in arxiv:2207.03949 with standard gates
  • Loading branch information
damarkian authored Mar 28, 2024
2 parents adda716 + 7b0197d commit 2dc47f6
Show file tree
Hide file tree
Showing 6 changed files with 394 additions and 215 deletions.
4 changes: 4 additions & 0 deletions doc/source/tutorials/ansatz.rst
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ The following example demonstrates how the energy of the H2 molecule is affected
| 0.2 | 0.673325849299 |
-----------------------------
..
Placeholder for basis rotation ansatz documentation

.. _UCC Ansatz:

Unitary Coupled Cluster Ansatz
Expand Down
47 changes: 22 additions & 25 deletions examples/br_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
"""

import numpy as np
from qibo import Circuit, gates, models
from qibo.optimizers import optimize
from scipy.optimize import minimize

from qibochem.ansatz.basis_rotation import br_circuit
from qibochem.ansatz import basis_rotation
from qibochem.ansatz.hf_reference import hf_circuit
from qibochem.driver.molecule import Molecule
from qibochem.measurement.expectation import expectation
Expand All @@ -19,7 +20,6 @@
except ModuleNotFoundError:
mol.run_psi4()


# Diagonalize H_core to use as the guess wave function

# First, symmetric orthogonalization of overlap matrix S using np:
Expand Down Expand Up @@ -47,35 +47,32 @@
)


def electronic_energy(parameters):
"""
Loss function (Electronic energy) for the basis rotation ansatz
"""
circuit = hf_circuit(mol.nso, mol.nelec)
circuit += br_circuit(mol.nso, parameters, mol.nelec)

return expectation(circuit, hamiltonian)
def basis_rotation_circuit(mol, parameters=0.0):

nqubits = mol.nso
occ = range(0, mol.nelec)
vir = range(mol.nelec, mol.nso)

# Build a basis rotation_circuit
params = np.random.rand(mol.nelec * (mol.nso - mol.nelec)) # n_occ * n_virt
U, theta = basis_rotation.unitary(occ, vir, parameters=parameters)

best, params, extra = optimize(electronic_energy, params)
gate_angles, final_U = basis_rotation.givens_qr_decompose(U)
gate_layout = basis_rotation.basis_rotation_layout(nqubits)
gate_list, ordered_angles = basis_rotation.basis_rotation_gates(gate_layout, gate_angles, theta)

print("\nResults using Qibo optimize:")
print(f" HF energy: {mol.e_hf:.8f}")
print(f"VQE energy: {best:.8f} (Basis rotation ansatz)")
# print()
# print("Optimized parameters:", params)
circuit = Circuit(nqubits)
for _i in range(mol.nelec):
circuit.add(gates.X(_i))
circuit.add(gate_list)

return circuit, gate_angles

params = np.random.rand(mol.nelec * (mol.nso - mol.nelec)) # n_occ * n_virt

result = minimize(electronic_energy, params)
best, params = result.fun, result.x
br_circuit, qubit_parameters = basis_rotation_circuit(mol, parameters=0.1)
vqe = models.VQE(br_circuit, hamiltonian)
vqe_result = vqe.minimize(qubit_parameters)

print("\nResults using scipy.optimize:")
print(f" HF energy: {mol.e_hf:.8f}")
print(f"VQE energy: {best:.8f} (Basis rotation ansatz)")
# print()
# print("Optimized parameters:", params)
print(f"VQE energy: {vqe_result[0]:.8f} (Basis rotation ansatz)")
print()
print("Optimized qubit parameters:\n", vqe_result[1])
print("Optimizer message:\n", vqe_result[2])
9 changes: 4 additions & 5 deletions src/qibochem/ansatz/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from qibochem.ansatz.basis_rotation import (
br_circuit,
givens_rotation_gate,
givens_rotation_parameters,
swap_matrices,
unitary_rot_matrix,
basis_rotation_gates,
basis_rotation_layout,
givens_qr_decompose,
unitary,
)
from qibochem.ansatz.hardware_efficient import hea
from qibochem.ansatz.hf_reference import bk_matrix, bk_matrix_power2, hf_circuit
Expand Down
Loading

0 comments on commit 2dc47f6

Please sign in to comment.