Eigra

Spins and qubits

A spin has no wavefunction on a grid. Its state is a short vector of complex numbers, and its Hamiltonian is a small matrix. That matrix can be diagonalised outright, so the evolution here is exact: there is no dt to choose.

Everything for these systems lives in eigora.qm.discrete.

Rabi oscillations

Drive a two-level system and its population swings back and forth. Rabi builds the Hamiltonian from the detuning and the rabi_frequency, and evolve takes an initial state and a list of times:

import numpy as np
import matplotlib.pyplot as plt

from eigora.qm.discrete import Rabi, evolve

detuning = 0.0
rabi_frequency = 1.0

system = Rabi(detuning=detuning, rabi_frequency=rabi_frequency)
times = np.linspace(0.0, 20.0, 400)
evolution = evolve(system, np.array([1.0, 0.0]), times)

# The probability of having left the initial state |0>.
excited = np.abs(evolution.psi[:, 1]) ** 2

plt.plot(times, excited, label="transfer probability")
plt.axhline(system.max_transfer, color="gray", linestyle="--", label="max_transfer")
plt.ylim(0, 1.05)
plt.xlabel("t")
plt.ylabel("probability")
plt.legend()
plt.show()
0.0
1.0
Runs in your browser. The first run downloads up to 40 MB.

With zero detuning, the population transfers completely. Increase the detuning and the oscillations get faster but never reach 1. The peak is max_transfer, the dashed line.

The physics behind this is in the ammonia maser module.

Simulating measurements

An observable such as SZ gives you the possible outcomes and their probabilities, and measure draws one at random. Try changing theta and n_shots:

import numpy as np

from eigora.qm.discrete import SZ

theta = 1.57
n_shots = 1000

# A spin pointing at angle theta from the z axis.
state = np.array([np.cos(theta / 2), np.sin(theta / 2)])

print("Born probabilities:")
for outcome in SZ.outcomes(state):
    print(f"  {outcome.value:+.0f}: {outcome.probability:.3f}")

rng = np.random.default_rng()
results = [SZ.measure(state, rng)[0].value for _ in range(n_shots)]

print(f"\nFraction of +1 in {n_shots} measurements: {np.mean(np.array(results) > 0):.3f}")
print(f"Average: {np.mean(results):+.3f}   Expectation value: {SZ.expectation(state):+.3f}")
Runs in your browser. The first run downloads up to 40 MB.

Each run gives slightly different counts, since these are random draws. With more shots, the fractions settle on the Born probabilities. The spin and measurement modules explain why.

Two coupled spins

HeisenbergChain couples spins together, and embed places a single-spin operator on one site of a larger system:

import numpy as np

from eigora.qm.discrete import HeisenbergChain, SZ, embed

# Two coupled spins. Try coupling=-1.0, or add field=0.5.
chain = HeisenbergChain(n_sites=2)
print("Energies:", np.round(chain.eigenenergies(), 3))

dims = (2, 2)
total_sz = embed(SZ, 0, dims) + embed(SZ, 1, dims)
print("Total SZ:", np.round(total_sz.eigenvalues(), 3))

# Equal weights on |01> and |10>: one of the triplet states.
state = np.array([0.0, 1.0, 1.0, 0.0]) / np.sqrt(2)
print("Energy of this state:", round(chain.expectation(state), 3))
Runs in your browser. The first run downloads up to 40 MB.

The energies are one singlet at −3-3 and a triplet at +1+1. Note that embed adds the two spins' values to give the total spin, unlike a plain tensor product, which would multiply them.

Exact diagonalisation has a limit: the matrix doubles in size with every spin, so this approach runs out of room at around a dozen spins.