use super::{ControlChannel, HamiltonianModel, Source};
use crate::error::{Error, Result};
use crate::linalg::{CMat, RMat};
use crate::setup::spin_ops;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Coupling {
Z,
Xy,
Xyz,
}
impl Coupling {
pub fn parse(s: &str) -> Result<Coupling> {
match s.to_uppercase().as_str() {
"Z" => Ok(Coupling::Z),
"XY" => Ok(Coupling::Xy),
"XYZ" => Ok(Coupling::Xyz),
_ => Err(Error::Config(format!("coupling_type \"{s}\" is not one of Z, XY, XYZ"))),
}
}
}
pub struct SpinChain {
n: usize,
coupling: Coupling,
ops: Vec<[CMat<f64>; 3]>,
}
impl SpinChain {
pub fn new(n: usize, coupling: Coupling) -> Self {
SpinChain {
n,
coupling,
ops: spin_ops(n),
}
}
}
impl HamiltonianModel for SpinChain {
fn name(&self) -> &'static str {
"spin_chain"
}
fn dim(&self) -> usize {
1 << self.n
}
fn drift(&self, offsets: &[f64], coupling: Option<&RMat<f64>>) -> Result<CMat<f64>> {
let d = self.dim();
let mut h = CMat::<f64>::zeros(d, d);
for (q, &delta) in offsets.iter().enumerate().take(self.n) {
h.axpy_re(delta, &self.ops[q][2]);
}
if let (Some(j), true) = (coupling, self.n > 1) {
for a in 0..self.n {
for b in a + 1..self.n {
let jab = j.get(a, b);
if jab == 0.0 {
continue;
}
let axes: &[usize] = match self.coupling {
Coupling::Z => &[2],
Coupling::Xy => &[0, 1],
Coupling::Xyz => &[0, 1, 2],
};
for &k in axes {
h.axpy_re(jab, &self.ops[a][k].matmul(&self.ops[b][k])?);
}
}
}
}
Ok(h)
}
fn control_ops(&self) -> Vec<CMat<f64>> {
self.ops.iter().flat_map(|o| [o[0].clone(), o[1].clone()]).collect()
}
fn control_channels(&self) -> Vec<ControlChannel> {
(0..self.n)
.flat_map(|q| {
[
ControlChannel::drive(q, Source::Cx),
ControlChannel::drive(q, Source::Cy),
]
})
.collect()
}
}