use super::{ControlChannel, HamiltonianModel, Source, embed_local};
use crate::autodiff::C;
use crate::error::{Error, Result};
use crate::linalg::{CMat, Mat, RMat};
pub struct DuffingTransmon {
n: usize,
alpha: Vec<f64>,
a: Vec<CMat<f64>>,
adag: Vec<CMat<f64>>,
number: Vec<CMat<f64>>,
x: Vec<CMat<f64>>,
y: Vec<CMat<f64>>,
p: CMat<f64>,
}
impl DuffingTransmon {
pub fn new(n: usize, alpha: Vec<f64>) -> Result<Self> {
if alpha.len() != n {
return Err(Error::Config(format!(
"anharmonicities has {} entries for {n} qubits",
alpha.len()
)));
}
let (o, s2) = (C::new(0.0, 0.0), C::new(2f64.sqrt(), 0.0));
let l = C::new(1.0, 0.0);
let a_local = Mat::from_vec(3, 3, vec![o, l, o, o, o, s2, o, o, o])?;
let adag_local = a_local.adjoint();
let n_local = adag_local.matmul(&a_local)?;
let x_local = a_local.add(&adag_local)?.scale_re(0.5);
let i = C::new(0.0, 1.0);
let y_local = a_local.scale(-i).add(&adag_local.scale(i))?.scale_re(0.5);
let site = |m: &CMat<f64>| (0..n).map(|q| embed_local(m, q, n)).collect::<Vec<_>>();
let d_comp = 1usize << n;
let d_full = 3usize.pow(n as u32);
let mut p = CMat::<f64>::zeros(d_full, d_comp);
for c in 0..d_comp {
let full: usize = (0..n).filter(|b| c & (1 << b) != 0).map(|b| 3usize.pow(b as u32)).sum();
p.set(full, c, l);
}
Ok(DuffingTransmon {
n,
alpha,
a: site(&a_local),
adag: site(&adag_local),
number: site(&n_local),
x: site(&x_local),
y: site(&y_local),
p,
})
}
pub fn leakage(&self, psi: &CMat<f64>) -> f64 {
let comp: f64 = (0..self.p.cols)
.map(|c| {
let r = (0..self.p.rows).find(|&r| self.p.get(r, c).re != 0.0).unwrap_or(0);
psi.get(r, 0).norm_sqr()
})
.sum();
1.0 - comp
}
}
impl HamiltonianModel for DuffingTransmon {
fn name(&self) -> &'static str {
"duffing_transmon"
}
fn dim(&self) -> usize {
3usize.pow(self.n as u32)
}
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.number[q]);
let nn = self.number[q].matmul(&self.number[q])?.sub(&self.number[q])?;
h.axpy_re(self.alpha[q] / 2.0, &nn);
}
if let (Some(g), true) = (coupling, self.n > 1) {
for i in 0..self.n {
for j in i + 1..self.n {
let gij = g.get(i, j);
if gij != 0.0 {
let hop = self.adag[i]
.matmul(&self.a[j])?
.add(&self.a[i].matmul(&self.adag[j])?)?;
h.axpy_re(gij, &hop);
}
}
}
}
Ok(h)
}
fn control_ops(&self) -> Vec<CMat<f64>> {
(0..self.n)
.flat_map(|q| [self.x[q].clone(), self.y[q].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()
}
fn embed_state(&self, psi: &CMat<f64>) -> Result<CMat<f64>> {
self.p.matmul(psi)
}
fn embed_density(&self, rho: &CMat<f64>) -> Result<CMat<f64>> {
self.p.matmul(rho)?.matmul(&self.p.adjoint())
}
fn embed_gate(&self, g: &CMat<f64>) -> Result<CMat<f64>> {
let pp = self.p.matmul(&self.p.adjoint())?;
let rest = CMat::<f64>::identity(self.dim()).sub(&pp)?;
self.p.matmul(g)?.matmul(&self.p.adjoint())?.add(&rest)
}
}