-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
174 additions
and
289 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
venv | ||
venv/ | ||
__pycache__ | ||
.DS_Store | ||
notconsider | ||
pruebas/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,83 +1,39 @@ | ||
from .base import vqe_base | ||
from quantumsim.lattice import lattice, custom_lattice | ||
from pennylane import FermiC, FermiA | ||
import pennylane as qml | ||
from pennylane import numpy as np | ||
from qiskit_nature.second_q.mappers import JordanWignerMapper | ||
import numpy as np | ||
from qiskit_nature.second_q.operators import FermionicOp | ||
|
||
|
||
""" | ||
Clase para construir el hamiltoniano usado en el proceso de VQE, hereda metodos de la clase | ||
vqe_base | ||
""" | ||
class vqe_fermihubbard(vqe_base): | ||
class fermihubbard_model(vqe_base): | ||
|
||
""" | ||
Constructor de la clase | ||
params: diccionario con los parametros del hamiltoniano | ||
lat: diccionario con los parametros de la lattice | ||
""" | ||
def __init__(self, params, lat): | ||
self.qubits = params["sites"]*2 | ||
fermi_sentence = 0.0 | ||
def __init__(self, params): | ||
self.qubits = int( 2*params['sites'] ) | ||
pair_indexes = params['indexs'] | ||
hopping = params['hopping'] | ||
onsite = params['onsite'] | ||
|
||
x,y = lat['size'] | ||
if lat['lattice'] != 'custom': | ||
lattice_edge, lattice_node = lattice(lat) | ||
else: | ||
lattice_edge, lattice_node = custom_lattice(lat) | ||
|
||
#Construir terminos asociados al termino -t | ||
hopping = -params["hopping"] | ||
fermi_hopping = 0.0 | ||
for pair in lattice_edge: | ||
p1, p2 = pair | ||
fermi_hopping += FermiC(2*(y*p1[0] + p1[1]))*FermiA(2*(y*p2[0] + p2[1])) + FermiC(2*(y*p2[0] + p2[1]))*FermiA(2*(y*p1[0] + p1[1])) | ||
fermi_hopping += FermiC(2*(y*p1[0] + p1[1])+1)*FermiA(2*(y*p2[0] + p2[1])+1) + FermiC(2*(y*p2[0] + p2[1])+1)*FermiA(2*(y*p1[0] + p1[1])+1) | ||
|
||
fermi_sentence = hopping*fermi_hopping | ||
second_op_f = {} | ||
for t,p in zip(hopping, pair_indexes): | ||
# 0,1 | ||
second_op_f[f"+_{2*p[0]} -_{2*p[1]}"] = t | ||
second_op_f[f"-_{2*p[0]} +_{2*p[1]}"] = -t | ||
|
||
#Construir terminos asociados al potencial U | ||
if 'U' in params: | ||
u_potential = params["U"] | ||
fermi_u = 0.0 | ||
for node in lattice_node: | ||
p1, p2 = node | ||
fermi_u += FermiC(y*p1 + 2*p2)*FermiA(y*p1 + 2*p2)*FermiC(y*p1 + 2*p2+1)*FermiA(y*p1 + 2*p2+1) | ||
fermi_sentence += u_potential*fermi_u | ||
second_op_f[f"+_{2*p[0] +1} -_{2*p[1] +1}"] = t | ||
second_op_f[f"-_{2*p[0] +1} +_{2*p[1] +1}"] = -t | ||
|
||
#Construir terminos asociados al campo electroico E | ||
if 'E' in params: | ||
e_field = params["E"] | ||
fermi_e = 0.0 | ||
for node in lattice_node: | ||
p1, p2 = node | ||
fermi_e += FermiC(y*p1 + 2*p2)*FermiA(y*p1 + 2*p2) | ||
fermi_e += FermiC(y*p1 + 2*p2+1)*FermiA(y*p1 + 2*p2+1) | ||
fermi_sentence += e_field*fermi_e | ||
for u,p in zip( onsite, [i for i in range(params['sites'])] ): | ||
second_op_f[f"+_{2*p} -_{2*p} +_{2*p +1} -_{2*p +1}"] = u | ||
op = FermionicOp( second_op_f, num_spin_orbitals=self.qubits ) | ||
mapper = JordanWignerMapper() | ||
self.hamiltonian = mapper.map(op) | ||
|
||
#Construir terminos asociados al potencial V | ||
if 'V' in params: | ||
v_potencial = params["V"] | ||
fermi_v = 0.0 | ||
for pair in lattice_edge: | ||
p1, p2 = pair | ||
n_i = FermiC(2*(y*p1[0] + p1[1]))*FermiA(2*(y*p1[0] + p1[1])) + FermiC(2*(y*p1[0] + p1[1]) +1)*FermiA(2*(y*p1[0] + p1[1]) +1) | ||
n_j = FermiC(2*(y*p2[0] + p2[1]))*FermiA(2*(y*p2[0] + p2[1])) + FermiC(2*(y*p2[0] + p2[1]) +1)*FermiA(2*(y*p2[0] + p2[1]) +1) | ||
fermi_v += n_i*n_j | ||
fermi_sentence += v_potencial*fermi_v | ||
|
||
#Transformar los terminos de segunda cuantizacion a espines | ||
coeff, terms = qml.jordan_wigner( fermi_sentence, ps=True ).hamiltonian().terms() | ||
|
||
#Eliminar terminos cuyos coefficientes son 0 | ||
to_delete = [i for i,c in enumerate(coeff) if np.abs(c)<1e-10 ] | ||
for i,_ in enumerate(coeff): | ||
if isinstance(terms[i], qml.Identity): | ||
terms[i] = qml.Identity(wires=[0]) | ||
|
||
to_delete = -np.sort(-np.array(to_delete)) | ||
for index in to_delete: | ||
coeff.pop(index) | ||
terms.pop(index) | ||
|
||
#Almacenar hamiltoniano | ||
self.hamiltonian = qml.Hamiltonian(np.real(np.array(coeff)), terms) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,37 @@ | ||
from .base import vqe_base | ||
from pennylane import qchem | ||
from qiskit_nature.units import DistanceUnit | ||
from qiskit_nature.second_q.drivers import PySCFDriver | ||
from qiskit_nature.second_q.mappers import JordanWignerMapper | ||
import numpy as np | ||
|
||
""" | ||
Clase para construir el hamiltoniano usado en el proceso de VQE, hereda metodos de la clase | ||
vqe_base | ||
""" | ||
class vqe_molecular(vqe_base): | ||
""" | ||
Variables de la clase | ||
""" | ||
mapping= 'jordan_wigner' | ||
charge= 0 | ||
mult= 1 | ||
basis='sto-3g' | ||
method='dhf' | ||
active_electrons = None | ||
active_orbitals = None | ||
|
||
class molecule_model(vqe_base): | ||
""" | ||
Constructor de la clase | ||
symbols: lista con los elementos de la molecula | ||
coordinates: vector con las posiciones de los elementos en el espacio | ||
params: diccionario con los parametros del hamiltoniano | ||
""" | ||
def __init__(self, symbols, coordinates, params= None): | ||
if 'mapping' in params: | ||
self.mapping = params['mapping'] | ||
|
||
if 'charge' in params: | ||
self.charge = params['charge'] | ||
def __init__(self, params): | ||
|
||
symbols = params['symbols'] | ||
coor = params['coor'] | ||
|
||
atom_string = "" | ||
for i,s in enumerate(symbols): | ||
atom_string += f"{s} {coor[3*i]} {coor[3*i + 1]} {coor[3*i +2]};" | ||
|
||
if 'mult' in params: | ||
self.mult = params['mult'] | ||
driver = PySCFDriver( | ||
atom=atom_string, | ||
unit=DistanceUnit.ANGSTROM, | ||
basis=params['basis'], | ||
charge=params['charge'], | ||
spin=params['spin'] | ||
) | ||
|
||
if 'basis' in params: | ||
self.basis = params['basis'] | ||
problem = driver.run() | ||
second_q_op, _ = problem.second_q_ops() | ||
mapper = JordanWignerMapper() | ||
|
||
if 'method' in params: | ||
self.method = params['method'] | ||
|
||
if 'active_electrons' in params: | ||
self.active_electrons = params['active_electrons'] | ||
self.hamiltonian = mapper.map(second_q_op) | ||
|
||
if 'active_orbitals' in params: | ||
self.active_orbitals = params['active_orbitals'] | ||
|
||
|
||
self.hamiltonian, self.qubits = qchem.molecular_hamiltonian( | ||
symbols= symbols, | ||
coordinates= coordinates, | ||
mapping= self.mapping, | ||
charge= self.charge, | ||
mult= self.mult, | ||
basis= self.basis, | ||
method= self.method, | ||
active_electrons=self.active_electrons, | ||
active_orbitals=self.active_orbitals) | ||
|
Oops, something went wrong.