use super::{ControlChannel, HamiltonianModel, Source};
use crate::error::{Error, Result};
use crate::linalg::{CMat, RMat};
use crate::setup::spin_ops;
pub struct Superconducting {
n: usize,
coupling_type: String,
anharmonicities: Option<Vec<f64>>,
zz_crosstalk: Option<RMat<f64>>,
stark: Option<Vec<f64>>,
ops: Vec<[CMat<f64>; 3]>,
}
impl Superconducting {
pub fn new(
n: usize,
coupling_type: String,
anharmonicities: Option<Vec<f64>>,
zz_crosstalk: Option<RMat<f64>>,
stark: Option<Vec<f64>>,
) -> Self {
Superconducting {
n,
coupling_type,
anharmonicities,
zz_crosstalk,
stark,
ops: spin_ops(n),
}
}
}
impl HamiltonianModel for Superconducting {
fn name(&self) -> &'static str {
"superconducting"
}
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 self.n < 2 {
return Ok(h);
}
let g = coupling.filter(|_| self.n > 1);
if let (Some(g), true) = (g, self.coupling_type.contains("XY")) {
for a in 0..self.n {
for b in a + 1..self.n {
let gab = g.get(a, b);
if gab != 0.0 {
let xx = self.ops[a][0].matmul(&self.ops[b][0])?;
let yy = self.ops[a][1].matmul(&self.ops[b][1])?;
h.axpy_re(2.0 * gab, &xx.add(&yy)?);
}
}
}
}
if self.coupling_type.contains("ZZ") {
for a in 0..self.n {
for b in a + 1..self.n {
let zeta = match (&self.zz_crosstalk, &self.anharmonicities, g) {
(Some(zz), _, _) => zz.get(a, b),
(None, Some(alpha), Some(g)) => {
zz_estimate(g.get(a, b), offsets[a] - offsets[b], alpha[a], alpha[b])?
}
_ => 0.0,
};
if zeta != 0.0 {
h.axpy_re(zeta, &self.ops[a][2].matmul(&self.ops[b][2])?);
}
}
}
}
Ok(h)
}
fn control_ops(&self) -> Vec<CMat<f64>> {
self.ops
.iter()
.flat_map(|o| {
let mut v = vec![o[0].clone(), o[1].clone()];
if self.stark.is_some() {
v.push(o[2].clone());
}
v
})
.collect()
}
fn control_channels(&self) -> Vec<ControlChannel> {
(0..self.n)
.flat_map(|q| {
let mut v = vec![
ControlChannel::drive(q, Source::Cx),
ControlChannel::drive(q, Source::Cy),
];
if let Some(s) = &self.stark {
v.push(ControlChannel {
qubit: q,
source: Source::Power,
rabi_power: 2,
coeff: -s[q],
});
}
v
})
.collect()
}
}
const MAX_MIXING: f64 = 0.1;
fn zz_estimate(g: f64, detuning: f64, alpha_a: f64, alpha_b: f64) -> Result<f64> {
if g == 0.0 {
return Ok(0.0);
}
let (to_20, to_02) = (detuning + alpha_a, detuning - alpha_b);
let mixing = 2f64.sqrt() * g.abs() / to_20.abs().min(to_02.abs());
if mixing > MAX_MIXING || mixing.is_nan() {
return Err(Error::Config(format!(
"the ZZ estimate from the anharmonicities is second order and needs |11> far from |20> and |02>, but \
their mixing ratio is {mixing:.2}, above the {MAX_MIXING} this accepts; give zz_crosstalk instead"
)));
}
Ok(2.0 * g * g * (alpha_a + alpha_b) / (to_20 * to_02))
}