lumeq.spins package
Submodules
lumeq.spins.circuit module
- beta_layer(qubits, beta)[source]
Define the beta layer of the circuit for mixer.
- Parameters:
qubits – Cirq qubit objects.
beta – Rotation angle of the initial Hamiltonian
sum_i X_i.
- Returns:
Rotation-gate operations.
- Return type:
iterable
- create_circuit(qubits, gamma=None, beta=None, nlayers=0, **kwargs)[source]
Create a quantum circuit with Hadamard gates on each qubit for equal superposition as real initial state.
- Parameters:
qubits – Qubit container.
gamma – Symbol or value used for the
U(gamma, C)operator.beta – Symbol or value used for the
U(beta, B)operator.nlayers (int) – Number of circuit layers.
**kwargs – Additional parameters such as coupling strengths and magnetic fields.
- Returns:
Constructed circuit.
- Return type:
cirq.Circuit
- gamma_layer(qubits, gamma, js, hs)[source]
Define the gamma layer of the circuit with cost Hamiltonian.
- Parameters:
qubits – Cirq qubit objects.
gamma – Rotation angle of the target Hamiltonian.
js – Coupling constants between qubits.
hs – Magnetic-field constants.
- Returns:
Rotation-gate operations.
- Return type:
iterable
lumeq.spins.infinite_mps_block module
- apply_gate_on_mps(gate_ab, s_ab, s_ba, mps_a, mps_b, chi, tol=1e-07)[source]
Apply a two-site evolution gate to the AB MPS tensors.
- Parameters:
gate_ab (numpy.ndarray) – Two-site gate on the AB pattern.
s_ab (numpy.ndarray) – Bond weights from site A to site B.
s_ba (numpy.ndarray) – Bond weights from site B to site A.
mps_a (numpy.ndarray) – MPS tensor on site A.
mps_b (numpy.ndarray) – MPS tensor on site B.
chi (int) – Maximum retained bond dimension after truncation.
tol (float) – Lower bound used when inverting small singular values.
- Returns:
Updated
(s_ab, mps_a, mps_b)after applying the gate.- Return type:
tuple
- build_evolution_gate(ham, tau, itype='imag')[source]
Build evolution gate from given Hamiltonian
- Parameters:
ham (numpy.ndarray) – Hamiltonian matrix.
tau (float) – Time-evolution step.
itype (str) – Evolution type. Use
'imag'for imaginary-time evolution and'real'for real-time evolution.
- Returns:
Matrix exponential of the evolution operator.
- Return type:
numpy.ndarray
- contract_from_left(mu_ba, s_ab, s_ba, mps_a, mps_b, pick_eig='LM')[source]
Update the left environment density matrix for a two-site unit cell.
- Parameters:
mu_ba (numpy.ndarray) – Left environment density matrix on the BA bond.
s_ab (numpy.ndarray) – Bond weights from site A to site B.
s_ba (numpy.ndarray) – Bond weights from site B to site A.
mps_a (numpy.ndarray) – MPS tensor on site A.
mps_b (numpy.ndarray) – MPS tensor on site B.
pick_eig (str) – Eigenvalue selection mode passed to
eigsh.
- Returns:
Updated
(mu_ba, mu_ab)density matrices.- Return type:
tuple
- contract_from_right(nu_ab, s_ab, s_ba, mps_a, mps_b, pick_eig='LM')[source]
Update the right environment density matrix for a two-site unit cell.
- Parameters:
nu_ab (numpy.ndarray) – Right environment density matrix on the AB bond.
s_ab (numpy.ndarray) – Bond weights from site A to site B.
s_ba (numpy.ndarray) – Bond weights from site B to site A.
mps_a (numpy.ndarray) – MPS tensor on site A.
mps_b (numpy.ndarray) – MPS tensor on site B.
pick_eig (str) – Eigenvalue selection mode passed to
eigsh.
- Returns:
Updated
(nu_ab, nu_ba)density matrices.- Return type:
tuple
- evaluate_energy_mps(mpo_ab, mpo_ba, s_ab, s_ba, mps_a, mps_b)[source]
Evaluate the average two-site energy directly from the MPS tensors.
- Parameters:
mpo_ab (numpy.ndarray) – MPO tensor for the AB pattern.
mpo_ba (numpy.ndarray) – MPO tensor for the BA pattern.
s_ab (numpy.ndarray) – Bond weights from site A to site B.
s_ba (numpy.ndarray) – Bond weights from site B to site A.
mps_a (numpy.ndarray) – MPS tensor on site A.
mps_b (numpy.ndarray) – MPS tensor on site B.
- Returns:
Average of the AB and BA energies.
- Return type:
float
- evaluate_energy_rdm(mpo_ab, mpo_ba, rho_ab, rho_ba)[source]
Get energy of the two sites A and B using the density matrix from get_mps_2rdm function.
- Parameters:
mpo_ab (matrix product operators (MPO) of AB and BA)
mpo_ba (matrix product operators (MPO) of AB and BA)
rho_ab (density matrix of AB and BA)
rho_ba (density matrix of AB and BA)
- Returns:
averaged energy of AB and BA
- get_mps_2rdm(s_ab, s_ba, mps_a, mps_b)[source]
Build the two-site reduced density matrices for the unit cell.
- Parameters:
s_ab (numpy.ndarray) – Bond weights from site A to site B.
s_ba (numpy.ndarray) – Bond weights from site B to site A.
mps_a (numpy.ndarray) – MPS tensor on site A.
mps_b (numpy.ndarray) – MPS tensor on site B.
- Returns:
Reduced density matrices
(rho_ab, rho_ba).- Return type:
tuple
- normalize_mps(s_left, s_right, mps)[source]
Normalize a single-site MPS tensor.
- Parameters:
s_left (numpy.ndarray) – Left bond weights.
s_right (numpy.ndarray) – Right bond weights.
mps (numpy.ndarray) – MPS tensor to normalize.
- Returns:
Normalized MPS tensor.
- Return type:
numpy.ndarray
- normalize_mps_2(s_ab, s_ba, mps_a, mps_b)[source]
Normalize both site tensors in a two-site unit cell.
- Parameters:
s_ab (numpy.ndarray) – Bond weights from site A to site B.
s_ba (numpy.ndarray) – Bond weights from site B to site A.
mps_a (numpy.ndarray) – MPS tensor on site A.
mps_b (numpy.ndarray) – MPS tensor on site B.
- Returns:
Normalized
(mps_a, mps_b).- Return type:
tuple
- run_tebd(ham_ab, ham_ba, mps_a, mps_b, s_ab, s_ba, mu_ba=None, nu_ab=None, tau=0.1, itype='imag', nmax=1000, midstep=10)[source]
Run TEBD for an infinite two-site unit-cell MPS.
- update_ortho_mps(rho_left, rho_right, weight, mps_left, mps_right, tol=1e-12)[source]
Bring the two-site MPS linker into canonical form.
- Parameters:
rho_left (numpy.ndarray) – Left environment density matrix.
rho_right (numpy.ndarray) – Right environment density matrix.
weight (numpy.ndarray) – Bond weights between the two sites.
mps_left (numpy.ndarray) – Left MPS tensor.
mps_right (numpy.ndarray) – Right MPS tensor.
tol (float) – Eigenvalue cutoff for the environment density matrices.
- Returns:
Updated
(weight, mps_left, mps_right).- Return type:
tuple
lumeq.spins.matrix_product_state module
- dmrg_opt_gs(h_mpo, mps=None, Tzip=None, nbond=None, pick_eig='SA', nmax=10)[source]
DMRG algorithm for ground-state optimization.
- Parameters:
h_mpo (list) – MPO Hamiltonian.
mps (list, optional) – Initial MPS.
Tzip (list, optional) – Initial zipper tensors.
nbond (int, optional) – Number of virtual bonds between neighboring MPS sites.
pick_eig (str, optional) – Method used to pick eigenvalues.
nmax (int, optional) – Number of DMRG sweep loops.
- Returns:
Site energies and optimized MPS.
- Return type:
tuple
- dmrg_sweep(h_mpo, mps, Tzip, pick_eig='SA')[source]
Perform one two-way DMRG sweep over the MPS chain.
- Parameters:
h_mpo (list) – MPO Hamiltonian of the system.
mps (list) – Current MPS tensors.
Tzip (list) – Zipper tensors used to contract the MPS with the MPO.
pick_eig (str) – Eigenvalue selection mode passed to
eigsh.
- Returns:
Site energies, updated MPS tensors, and updated zipper tensors.
- Return type:
tuple
Notes
Tzipis supplied as the right zipper with indices ordered from top to bottom so the routine can continue with a full left-to-right sweep followed by a full right-to-left sweep. The zipper contraction pattern for a local update is:| | | 3-l 3-m 1-n | | | T---2-*p*-1---O---2-*q*-2---T | | | 1-i 4-j 3-k | | |
The routine performs one full sweep:
left-to-right update of the local MPS tensors,
right-to-left update of the local MPS tensors,
zipper refresh at each site.
- get_hamil_from_mpo(h_mpo)[source]
Build the Hamiltonian in Hilbert space using the MPOs
- Parameters:
h_mpo (list) – MPO Hamiltonian.
- Returns:
Actual Hamiltonian matrix.
- Return type:
numpy.ndarray
- mpo_hamil_disordered(nsite, j=None, hz=None, model=None, spin_j=0.5, sigma=None)[source]
Construct MPO Hamiltonian at all the site with disordered parameters
- Returns:
MPO Hamiltonian.
- Return type:
list
- mpo_hamil_uniform(nsite, j=None, hz=None, Hl=None, model=None, spin_j=0.5, sigma=None)[source]
Construct uniform MPO Hamiltonian at all the site
- Returns:
MPO Hamiltonian.
- Return type:
list
- mpo_spin_correlation(mps, sigma=None, ns=None)[source]
Evaluate spin correlation function from MPS expval{sigma_{l}^{+} sigma_{l+1}^{-}}
- Parameters:
mps (list) – MPS tensors at the sites.
sigma (tuple, optional) – Pauli spin matrices.
ns (int or iterable, optional) – Sites on which the spin correlation is calculated.
- Returns:
Spin-correlation values between sites
nsandns+1.- Return type:
numpy.ndarray
- mps_canonical(ndims, nsite, seed=None, normalize='both')[source]
Create a random MPS in canonical form.
- Parameters:
ndims (array-like) – Leg dimensions
[nd, ns, nd]in the order[left, bottom, right]. The first and third values should be the same.nsite (int or tuple) – Number of sites in the system.
seed (int, optional) – Random seed for reproducibility.
normalize (str or int, optional) – Normalization mode. Supported values are
1or'left',2or'right', and'both'.
- Returns:
Generated matrix product state (MPS).
- Return type:
list
Notes
The bra MPS uses leg order
[left, bottom, right]. The ket MPS is its transpose with legs ordered as[right, top, left]. The left and right legs are virtual bonds to be contracted with adjacent MPS tensors, while the bottom or top legs are physical bonds to be contracted with the MPO.---1---Mt---3--- | 2 | | 2 | ---3---Mb---1---
- mps_canonical_left(mps, debug=0)[source]
Normalize MPS matrices from the left treat the first two dimensions as a vector to reduce the right leg numbers M1 * M2 = U*s*V^T * M2 = U * (s*V^T*M2) such that M1 is replaced by a smaller U
- mps_canonical_right(mps)[source]
Normalize MPS matrices from the right treat the last two dimensions as a vector to reduce the left leg numbers M1 * M2 = M1 * U*s*V^T = (M1*U*s) * V^T such that M2 is replaced by a smaller V^T
- zipper_from_left(Mt, O, Mb, Tl=array([[[1.]]]))[source]
Contract one MPS/MPO site into the left zipper tensor.
- Parameters:
Mt (numpy.ndarray) – Bra-side MPS tensor.
O (numpy.ndarray) – MPO tensor at the current site.
Mb (numpy.ndarray) – Ket-side MPS tensor.
Tl (numpy.ndarray) – Current left zipper tensor.
- Returns:
Updated left zipper tensor.
- Return type:
numpy.ndarray
Notes
Tlis ordered from bottom to top. The contraction attaches the bra tensor Mt at top, MPO tensor O at middle, and ket tensor Mb at bottom to the current left zipper:/---3---*q*---1---Mt---3-k-- | | | 2 | | | *l | | | 3 /---3-k-- | | | Tl--2---*p*---1---O----2-j-- = Tf--2-j-- | | | | 4 \---1-i-- | | | *m | | | 2 | | \---1---*n*---3---Mb---1-i---
- zipper_from_right(Mt, O, Mb, Tr=array([[[1.]]]))[source]
Contract one MPS/MPO site into the right zipper tensor.
- Parameters:
Mt (numpy.ndarray) – Bra-side MPS tensor.
O (numpy.ndarray) – MPO tensor at the current site.
Mb (numpy.ndarray) – Ket-side MPS tensor.
Tr (numpy.ndarray) – Current right zipper tensor.
- Returns:
Updated right zipper tensor.
- Return type:
numpy.ndarray
Notes
Tris ordered from top to bottom. This is the right-to-left analog ofzipper_from_left().--i-1---Mt---3---*q*---1---\ | | 2 | | | *l | | | 3 | --1-i--\ | | | --j-1---O----2---*p*---2---Tr = --2-j--Tf | | | 4 | --3-k--/ | | *m | | | 2 | | | --k-3---Mb---1---*n*---3---/
lumeq.spins.qaoa module
- class QAOA(nsite, **kwargs)[source]
Bases:
objectBase class for Quantum Approximate Optimization Algorithm models.
Notes
The variational state has the form
prod_k U(beta_k, B) U(gamma_k, C) |s>, where|s>is the equal-superposition initial state.U(gamma, C) = exp(-i * gamma * C)is the problem-Hamiltonian evolution andU(beta, B) = exp(-i * beta * B)is the mixer evolution.- property draw_circuit
Draw the quantum circuit.
- energy_evaluation(gamma, beta)[source]
Evaluate the energy expectation value for given gamma and beta on circuit.
- Parameters:
gamma (float) – Rotation angle of the target Hamiltonian.
beta (float) – Rotation angle of the initial Hamiltonian.
- Returns:
Energy expectation value of the simulated wavefunction.
- Return type:
float
- energy_expectation(wf, **kwargs)[source]
Calculate the energy expectation value for a given wavefunction.
- Parameters:
wf (numpy.ndarray) – Wavefunction array of size
2**(nrow*ncol).**kwargs – Additional keyword arguments.
- optimizer(gamma, beta, eps=0.001, max_steps=150, thresh=1e-05)[source]
Optimize rotation angles
- Parameters:
gamma (float) – Rotation angle for the target Hamiltonian.
beta (float) – Rotation angle for the initial Hamiltonian.
eps (float) – Finite-difference step size.
max_steps (int) – Maximum number of optimization steps.
thresh (float) – Convergence threshold for the energy.
- Returns:
Optimized
(gamma, beta, energy).- Return type:
tuple
- class QAOA_Ising(nsite, **kwargs)[source]
Bases:
QAOAE = - sum_{ij} sigma_i sigma_j - h_i sigma_i h is the magnetic field on each spin
lumeq.spins.spin_hamil module
- get_prod_spin_list(n, xs='all', j=0.5, np_matrix=True)[source]
Build spin operators acting on each site in the full Hilbert space.
Notes
This constructs the actual
sigma_{i,x}operators by looping over thenspins, placing the requested spin matrix on siteiand identity operators on all other sites. The returnedn * dmatrices form Hilbert-space operators or a basis in the full product space.
- hamil_heisenberg_1d(n, j, hz, np_matrix=True, spin_j=0.5, boundary='open')[source]
Build the 1D Heisenberg Hamiltonian in the full Hilbert space.
Notes
The Hamiltonian is
H = sum_i (j_{i,x} sigma_{i,x} sigma_{i+1,x} + j_{i,y} sigma_{i,y} sigma_{i+1,y} + j_{i,z} sigma_{i,z} sigma_{i+1,z}) + sum_i h_{z,i} sigma_{i,z}.When
j_x = j_y, the transverse part can be rewritten assigma_x sigma_x + sigma_y sigma_y = 0.5 * (sigma_p sigma_m + sigma_m sigma_p), which makes it convenient to use ladder operators and keep the matrices real.
- hamil_x_1d(n, j, np_matrix=True, spin_j=0.5, sigma=None, direction='x', boundary='open')[source]
separate one direction for efficiency
- hamil_xxz_1d(n, j, delta, hz, np_matrix=True, spin_j=0.5, sigma=None, boundary='open')[source]
j * (xx + yy + zz * delta) reimplement for efficiency using ladder operator by defalut
- hamil_xyz_1d(n, j, hz, np_matrix=True, spin_j=0.5, boundary='open')
Build the 1D Heisenberg Hamiltonian in the full Hilbert space.
Notes
The Hamiltonian is
H = sum_i (j_{i,x} sigma_{i,x} sigma_{i+1,x} + j_{i,y} sigma_{i,y} sigma_{i+1,y} + j_{i,z} sigma_{i,z} sigma_{i+1,z}) + sum_i h_{z,i} sigma_{i,z}.When
j_x = j_y, the transverse part can be rewritten assigma_x sigma_x + sigma_y sigma_y = 0.5 * (sigma_p sigma_m + sigma_m sigma_p), which makes it convenient to use ladder operators and keep the matrices real.