Skip to content

Commit

Permalink
Merge branch 'main' into qwc
Browse files Browse the repository at this point in the history
  • Loading branch information
chmwzc committed Apr 3, 2024
2 parents bdbe5cb + 2e60ecc commit d8a3fae
Show file tree
Hide file tree
Showing 14 changed files with 710 additions and 816 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ repos:
- id: check-yaml
- id: debug-statements
- repo: https://github.com/psf/black
rev: 24.2.0
rev: 24.3.0
hooks:
- id: black
args:
Expand All @@ -20,6 +20,6 @@ repos:
- id: isort
args: ["--profile", "black"]
- repo: https://github.com/asottile/pyupgrade
rev: v3.15.1
rev: v3.15.2
hooks:
- id: pyupgrade
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 d8a3fae

Please sign in to comment.