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 main (bump to 3.8 and PRs since v 0.3.3 release) #265

Merged
merged 15 commits into from
Jan 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/continuous_integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.7]
python-version: [3.8]

steps:
- uses: actions/checkout@v2
Expand Down Expand Up @@ -43,6 +43,7 @@ jobs:
pip install amazon-braket-sdk
pip install cirq
pip install projectq
pip install pennylane
if: always()

- name: Install Microsoft qsharp/qdk
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ RUN pip3 install tangelo-gc

# OPTIONAL: common dependencies (quantum circuit simulator and quantum cloud services)
# ====================================================================================
RUN pip3 install cirq amazon-braket-sdk qiskit qulacs projectq
RUN pip3 install cirq amazon-braket-sdk qiskit qulacs projectq pennylane
12 changes: 6 additions & 6 deletions examples/adapt.ipynb

Large diffs are not rendered by default.

36 changes: 26 additions & 10 deletions tangelo/algorithms/classical/ccsd_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
"""Class performing electronic structure calculation employing the CCSD method.
"""

import numpy as np
from pyscf import cc, lib
from pyscf.cc.ccsd_rdm import _make_rdm1, _make_rdm2, _gamma1_intermediates, _gamma2_outcore
from pyscf.cc.uccsd_rdm import (_make_rdm1 as _umake_rdm1, _make_rdm2 as _umake_rdm2,
_gamma1_intermediates as _ugamma1_intermediates, _gamma2_outcore as _ugamma2_outcore)

from tangelo.algorithms.electronic_structure_solver import ElectronicStructureSolver

Expand All @@ -37,8 +40,11 @@ class CCSDSolver(ElectronicStructureSolver):
def __init__(self, molecule):
self.cc_fragment = None

self.spin = molecule.spin

self.mean_field = molecule.mean_field
self.frozen = molecule.frozen_mos
self.uhf = molecule.uhf

def simulate(self):
"""Perform the simulation (energy calculation) for the molecule.
Expand Down Expand Up @@ -74,17 +80,27 @@ def get_rdm(self):
raise RuntimeError("CCSDSolver: Cannot retrieve RDM. Please run the 'simulate' method first")

# Solve the lambda equation and obtain the reduced density matrix from CC calculation
self.cc_fragment.solve_lambda()
t1 = self.cc_fragment.t1
t2 = self.cc_fragment.t2
l1 = self.cc_fragment.l1
l2 = self.cc_fragment.l2

d1 = _gamma1_intermediates(self.cc_fragment, t1, t2, l1, l2)
f = lib.H5TmpFile()
d2 = _gamma2_outcore(self.cc_fragment, t1, t2, l1, l2, f, False)

one_rdm = _make_rdm1(self.cc_fragment, d1, with_frozen=False)
two_rdm = _make_rdm2(self.cc_fragment, d1, d2, with_dm1=True, with_frozen=False)
l1, l2 = self.cc_fragment.solve_lambda(t1, t2)

if self.spin == 0 and not self.uhf:
d1 = _gamma1_intermediates(self.cc_fragment, t1, t2, l1, l2)
f = lib.H5TmpFile()
d2 = _gamma2_outcore(self.cc_fragment, t1, t2, l1, l2, f, False)

one_rdm = _make_rdm1(self.cc_fragment, d1, with_frozen=False)
two_rdm = _make_rdm2(self.cc_fragment, d1, d2, with_dm1=True, with_frozen=False)
else:
d1 = _ugamma1_intermediates(self.cc_fragment, t1, t2, l1, l2)
f = lib.H5TmpFile()
d2 = _ugamma2_outcore(self.cc_fragment, t1, t2, l1, l2, f, False)

one_rdm = _umake_rdm1(self.cc_fragment, d1, with_frozen=False)
two_rdm = _umake_rdm2(self.cc_fragment, d1, d2, with_dm1=True, with_frozen=False)

if not self.uhf:
one_rdm = np.sum(one_rdm, axis=0)
two_rdm = np.sum((two_rdm[0], 2*two_rdm[1], two_rdm[2]), axis=0)

return one_rdm, two_rdm
3 changes: 3 additions & 0 deletions tangelo/algorithms/classical/fci_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ class FCISolver(ElectronicStructureSolver):

def __init__(self, molecule):

if molecule.uhf:
raise NotImplementedError(f"SecondQuantizedMolecule that use UHF are not currently supported in {self.__class__.__name__}. Use CCSDSolver")

self.ci = None
self.norb = molecule.n_active_mos
self.nelec = molecule.n_active_electrons
Expand Down
14 changes: 13 additions & 1 deletion tangelo/algorithms/classical/tests/test_ccsd_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import unittest

from tangelo.algorithms.classical.ccsd_solver import CCSDSolver
from tangelo.molecule_library import mol_H2_321g, mol_Be_321g
from tangelo.molecule_library import mol_H2_321g, mol_Be_321g, mol_H4_sto3g_uhf_a1_frozen


# TODO: Can we test the get_rdm method on H2 ? How do we get our reference? Whole matrix or its properties?
Expand All @@ -29,6 +29,18 @@ def test_ccsd_h2(self):

self.assertAlmostEqual(energy, -1.1478300596229851, places=6)

def test_ccsd_h4_uhf_a1_frozen(self):
"""Test CCSDSolver against result from reference implementation."""

solver = CCSDSolver(mol_H4_sto3g_uhf_a1_frozen)
energy = solver.simulate()

self.assertAlmostEqual(energy, -1.95831052, places=6)

one_rdms, two_rdms = solver.get_rdm()

self.assertAlmostEqual(mol_H4_sto3g_uhf_a1_frozen.energy_from_rdms(one_rdms, two_rdms), -1.95831052, places=6)

def test_ccsd_be(self):
"""Test CCSDSolver against result from reference implementation."""

Expand Down
4 changes: 2 additions & 2 deletions tangelo/algorithms/variational/adapt_vqe_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def build(self):

self.n_spinorbitals = self.molecule.n_active_sos
self.n_electrons = self.molecule.n_active_electrons
self.spin = self.molecule.spin
self.spin = self.molecule.active_spin

# Compute qubit hamiltonian for the input molecular system
self.qubit_hamiltonian = fermion_to_qubit_mapping(fermion_operator=self.molecule.fermionic_hamiltonian,
Expand All @@ -153,7 +153,7 @@ def build(self):
# Build / set ansatz circuit.
ansatz_options = {"mapping": self.qubit_mapping, "up_then_down": self.up_then_down,
"reference_state": "HF" if self.ref_state is None else "zero"}
self.ansatz = ADAPTAnsatz(self.n_spinorbitals, self.n_electrons, ansatz_options)
self.ansatz = ADAPTAnsatz(self.n_spinorbitals, self.n_electrons, self.spin, ansatz_options)

# Build underlying VQE solver. Options remain consistent throughout the ADAPT cycles.
self.vqe_options = {"qubit_hamiltonian": self.qubit_hamiltonian,
Expand Down
10 changes: 5 additions & 5 deletions tangelo/algorithms/variational/tests/test_iqcc_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import unittest

from tangelo.algorithms.variational import iQCC_solver
from tangelo.molecule_library import mol_H2_sto3g, mol_H4_sto3g, mol_H4_cation_sto3g,\
from tangelo.molecule_library import mol_H2_sto3g, mol_H4_sto3g, mol_H4_sto3g_uhf_a1_frozen,\
mol_H4_doublecation_minao


Expand Down Expand Up @@ -69,12 +69,12 @@ def test_iqcc_h4(self):

self.assertAlmostEqual(iqcc_energy, -1.96259, places=4)

def test_iqcc_h4_cation(self):
"""Test the energy after 3 iterations for H4+ with generators limited to 8"""
def test_iqcc_h4_uhf(self):
"""Test the energy after 3 iterations for H4 uhf with 1 alpha orbital frozen and generators limited to 8"""

ansatz_options = {"max_qcc_gens": 8}

iqcc_options = {"molecule": mol_H4_cation_sto3g,
iqcc_options = {"molecule": mol_H4_sto3g_uhf_a1_frozen,
"qubit_mapping": "scbk",
"up_then_down": True,
"ansatz_options": ansatz_options,
Expand All @@ -85,7 +85,7 @@ def test_iqcc_h4_cation(self):
iqcc.build()
iqcc_energy = iqcc.simulate()

self.assertAlmostEqual(iqcc_energy, -1.639, places=3)
self.assertAlmostEqual(iqcc_energy, -1.95831, places=3)

def test_iqcc_h4_double_cation(self):
"""Test the energy after 1 iteration for H4+2"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
import unittest

from tangelo.algorithms.variational import TETRISADAPTSolver
from tangelo.molecule_library import mol_H2_sto3g
from tangelo.molecule_library import mol_H2_sto3g, mol_H4_sto3g_uhf_a1_frozen
from tangelo.toolboxes.ansatz_generator._unitary_majorana_cc import get_majorana_uccgsd_pool


class TETRISADAPTSolverTest(unittest.TestCase):
Expand All @@ -37,6 +38,19 @@ def test_single_cycle_tetris_adapt(self):

self.assertAlmostEqual(adapt_solver.optimal_energy, -1.13727, places=4)

def test_multiple_cycle_tetris_adapt_uhf(self):
"""Try running TETRISADAPTSolver with JKMN mapping and uhf H4 with majorana uccgsd pool for 7 iterations"""

opt_dict = {"molecule": mol_H4_sto3g_uhf_a1_frozen, "max_cycles": 7, "verbose": False,
"pool": get_majorana_uccgsd_pool, "pool_args": {"molecule": mol_H4_sto3g_uhf_a1_frozen},
"qubit_mapping": "JKMN"}
adapt_solver = TETRISADAPTSolver(opt_dict)
adapt_solver.build()
adapt_solver.simulate()

self.assertAlmostEqual(adapt_solver.optimal_energy, -1.95831, places=3)
self.assertTrue(adapt_solver.ansatz.n_var_params > 7)


if __name__ == "__main__":
unittest.main()
12 changes: 11 additions & 1 deletion tangelo/algorithms/variational/tests/test_vqe_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from tangelo.helpers.utils import installed_backends
from tangelo.linq.target import QiskitSimulator
from tangelo.algorithms import BuiltInAnsatze, VQESolver
from tangelo.molecule_library import mol_H2_sto3g, mol_H4_sto3g, mol_H4_cation_sto3g, mol_NaH_sto3g, mol_H4_sto3g_symm
from tangelo.molecule_library import mol_H2_sto3g, mol_H4_sto3g, mol_H4_cation_sto3g, mol_NaH_sto3g, mol_H4_sto3g_symm, mol_H4_sto3g_uhf_a1_frozen
from tangelo.toolboxes.ansatz_generator.uccsd import UCCSD
from tangelo.toolboxes.qubit_mappings.mapping_transform import fermion_to_qubit_mapping
from tangelo.toolboxes.molecular_computation.rdms import matricize_2rdm
Expand Down Expand Up @@ -299,6 +299,16 @@ def test_simulate_h4_open(self):
energy = vqe_solver.simulate()
self.assertAlmostEqual(energy, -1.6394, delta=1e-3)

def test_simulate_h4_open(self):
"""Run VQE on H4 molecule, with UCCSD ansatz, scbk qubit mapping, initial parameters, exact simulator """
vqe_options = {"molecule": mol_H4_sto3g_uhf_a1_frozen, "ansatz": BuiltInAnsatze.UCCSD, "qubit_mapping": "scbk",
"initial_var_params": [0.001]*15, "verbose": False, "up_then_down": True}
vqe_solver = VQESolver(vqe_options)
vqe_solver.build()

energy = vqe_solver.simulate()
self.assertAlmostEqual(energy, -1.95831, delta=1e-3)

def test_simulate_qmf_h4_open(self):
"""Run VQE on H4 + molecule, with QMF ansatz, JW qubit mapping, initial
parameters, exact simulator.
Expand Down
Loading