-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'spin_operators' of https://github.com/eclipse-qrisp/Qrisp…
… into spin_operators
- Loading branch information
Showing
26 changed files
with
1,140 additions
and
995 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
54 changes: 54 additions & 0 deletions
54
documentation/source/reference/Examples/GroundStateEnergyQPE.rst
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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
.. _GroundStateEnergyQPE: | ||
|
||
Ground State Energy with QPE | ||
============================ | ||
|
||
.. currentmodule:: qrisp | ||
|
||
Example Hydrogen | ||
================ | ||
|
||
We caluclate the ground state energy of the Hydrogen molecule with :ref:`Quantum Phase Estimation <QPE>`. | ||
|
||
Utilizing symmetries, one can find a reduced two qubit Hamiltonian for the Hydrogen molecule. Frist, we define the Hamiltonian, and compute the | ||
ground state energy classically. | ||
|
||
:: | ||
|
||
from qrisp import QuantumVariable, x, QPE | ||
from qrisp.operators.pauli.pauli import X,Y,Z | ||
import numpy as np | ||
|
||
# Hydrogen (reduced 2 qubit Hamiltonian) | ||
H = -1.05237325 + 0.39793742*Z(0) -0.39793742*Z(1) -0.0112801*Z(0)*Z(1) + 0.1809312*X(0)*X(1) | ||
E0 = H.ground_state_energy() | ||
# Yields: -1.85727502928823 | ||
|
||
In the following, we utilize the ``trotterization`` method of the :ref:`PauliHamiltonian` to obtain a function **U** that applies **Hamiltonian Simulation** | ||
via Trotterization. If we start in a state that is close to the ground state and apply :ref:`QPE`, we get an estimate of the ground state energy. | ||
|
||
:: | ||
|
||
# ansatz state | ||
qv = QuantumVariable(2) | ||
x(qv[0]) | ||
E1 = H.get_measurement(qv) | ||
E1 | ||
# Yields: -1.83858104676077 | ||
|
||
|
||
We calculate the ground state energy with quantum phase estimation. As the results of the phase estimation are modulo :math:`2\pi`, we subtract :math:`2\pi`. | ||
|
||
:: | ||
|
||
U = H.trotterization() | ||
|
||
qpe_res = QPE(qv,U,precision=10,kwargs={"steps":3},iter_spec=True) | ||
|
||
results = qpe_res.get_measurement() | ||
sorted_results= dict(sorted(results.items(), key=lambda item: item[1], reverse=True)) | ||
phi = list(sorted_results.items())[0][0] | ||
E_qpe = 2*np.pi*(phi-1) # Results are modulo 2*pi, therefore subtract 2*pi | ||
E_qpe | ||
# Yields: -1.8591847149174 |
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 |
---|---|---|
@@ -0,0 +1,84 @@ | ||
.. _IsingModel: | ||
|
||
Transverse Field Ising Model | ||
============================ | ||
|
||
.. currentmodule:: qrisp | ||
|
||
In this example, we study Hamiltonian dynamics of the transverse field Ising model defined by the Hamiltonian | ||
|
||
$$H = -J\\sum_{(i,j)\\in E}Z_iZ_j + B\\sum_{i\\in V}X_i$$ | ||
|
||
for a lattice graph $G=(V,E)$ and real parameters $J, B$. We investigate the total magnetization of the system as it evolves under the Hamiltonian. | ||
|
||
Here, we consider an Ising chain. | ||
|
||
:: | ||
|
||
import matplotlib.pyplot as plt | ||
import networkx as nx | ||
import numpy as np | ||
|
||
def generate_chain_graph(N): | ||
coupling_list = [[k,k+1] for k in range(N-1)] | ||
G = nx.Graph() | ||
G.add_edges_from(coupling_list) | ||
return G | ||
|
||
G = generate_chain_graph(6) | ||
|
||
We implement methods for creating the Ising Hamiltonian and the total magnetization observable for a given graph. | ||
|
||
:: | ||
|
||
from qrisp import QuantumVariable | ||
from qrisp.operators.pauli import X, Y, Z | ||
|
||
def create_ising_hamiltonian(G, J, B): | ||
H = sum(-J*Z(i)*Z(j) for (i,j) in G.edges()) + sum(B*X(i) for i in G.nodes()) | ||
return H | ||
|
||
def create_magnetization(G): | ||
H = (1/G.number_of_nodes())*sum(Z(i) for i in G.nodes()) | ||
return H | ||
|
||
With all the necessary ingredients, we conduct the experiment: For varying evolution times $T$: | ||
|
||
- Prepare the $\ket{0}^{\otimes N}$ state. | ||
|
||
- Perform **Hamiltonian simulation** via Trotterization. | ||
|
||
- Measure the total magnetization. | ||
|
||
:: | ||
|
||
T_values = np.arange(0, 2.0, 0.05) | ||
M_values = [] | ||
|
||
M = create_magnetization(G) | ||
|
||
for T in T_values: | ||
H = create_ising_hamiltonian(G,1.0,1.0) | ||
U = H.trotterization() | ||
|
||
qv = QuantumVariable(G.number_of_nodes()) | ||
U(qv,t=-T,steps=5) | ||
M_values.append(M.get_measurement(qv,precision=0.005)) | ||
|
||
Finally, we visualize the results. As expected, the total magnetization decreases in the presence of a transverse field with increasing evolution time $T$. | ||
|
||
:: | ||
|
||
import matplotlib.pyplot as plt | ||
plt.scatter(T_values, M_values, color='#6929C4', marker="o", linestyle='solid', s=10, label='Magnetization') | ||
plt.xlabel("Time", fontsize=15, color="#444444") | ||
plt.ylabel("Magnetization", fontsize=15, color="#444444") | ||
plt.legend(fontsize=12, labelcolor="#444444") | ||
plt.tick_params(axis='both', labelsize=12) | ||
plt.grid() | ||
plt.show() | ||
|
||
.. figure:: /_static/Ising_chain_N=6.png | ||
:alt: Ising chain | ||
:align: center | ||
|
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
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
Oops, something went wrong.