mod dissipation;
mod sampling;
mod states;
pub use dissipation::collapse_operators;
pub use states::{
canonical_gate, embed, gate, gate_names, pauli_ops, paulis, product_density, product_state, rotation, spin_ops,
};
use rand::SeedableRng;
use crate::autodiff::{C, LindbladOps};
use crate::basis::{QubitBasis, basis, envelope, initial_params, linspace, qubit_basis, raw_coefficients};
use crate::config::{Config, Coverage, Space, Targets};
use crate::error::{Error, Result};
use crate::hamiltonian::{ControlChannel, HamiltonianModel, model_from_config, upper_coupling};
use crate::linalg::{CMat, RMat};
use sampling::{QubitOffsets, coupling_instances, excitation_profile, rabi_instances, sample_offsets};
pub type Rng = rand_chacha::ChaCha8Rng;
pub fn resolve_seed(cfg: &Config) -> u64 {
cfg.seed.unwrap_or_else(|| {
let now = crate::time::SystemTime::now()
.duration_since(crate::time::UNIX_EPOCH)
.map(|d| d.as_nanos())
.unwrap_or(0);
let mut z = (now as u64 ^ (now >> 64) as u64).wrapping_add(0x9E37_79B9_7F4A_7C15);
z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9);
z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB);
z ^ (z >> 31)
})
}
#[derive(Clone, Debug, PartialEq)]
pub enum Evolution {
Hilbert,
Liouville,
Lindblad(LindbladOps),
}
#[derive(Clone, Debug, PartialEq)]
pub struct BatchElement {
pub h0: CMat<f64>,
pub rabi: Vec<f64>,
pub initial: CMat<f64>,
pub target: CMat<f64>,
pub initial_index: usize,
pub snapshot: usize,
pub rabi_index: usize,
}
pub struct Problem {
pub n_qubits: usize,
pub dim: usize,
pub n_pulse: usize,
pub dt: f64,
pub duration: f64,
pub times: Vec<f64>,
pub qubits: Vec<QubitBasis>,
pub modulation: CMat<f64>,
pub control_ops: Vec<CMat<f64>>,
pub channels: Vec<ControlChannel>,
pub batch: Vec<BatchElement>,
pub evolution: Evolution,
pub space: Space,
pub x0: Vec<f64>,
pub seed: u64,
pub target_fidelity: f64,
pub max_iter: usize,
pub algorithm: String,
pub initial_states: Vec<CMat<f64>>,
pub observables: Vec<[CMat<f64>; 3]>,
pub model: Box<dyn HamiltonianModel>,
pub rabi_max: Vec<f64>,
pub delta: Vec<f64>,
pub sw: Vec<f64>,
pub coupling: Option<RMat<f64>>,
pub n_snapshots: usize,
pub n_rabi: usize,
}
impl Problem {
pub fn n_params(&self) -> usize {
self.qubits.iter().map(|q| q.n_params).sum()
}
pub fn build(cfg: &Config) -> Result<Problem> {
Problem::build_with_seed(cfg, resolve_seed(cfg))
}
pub fn build_with_seed(cfg: &Config, seed: u64) -> Result<Problem> {
cfg.check()?;
let mut rng = Rng::seed_from_u64(seed);
let n = cfg.n_qubits();
let p = &cfg.parameters;
let two_pi = 2.0 * std::f64::consts::PI;
let per_qubit = |v: &Option<Vec<f64>>| -> Vec<f64> {
v.as_ref()
.map_or(vec![0.0; n], |v| v.iter().map(|x| two_pi * x).collect())
};
let scaled = |v: &[f64]| -> Vec<f64> { v.iter().map(|x| two_pi * x).collect() };
let duration = p.pulse_duration[0];
let n_pulse = p.point_in_pulse[0];
let dt = duration / n_pulse as f64;
let times: Vec<f64> = (0..n_pulse).map(|t| (t as f64 + 0.5) * dt).collect();
let bases = p.wf_type.iter().map(|name| basis(name)).collect::<Result<Vec<_>>>()?;
let raw: Vec<Vec<f64>> = p.n_para.iter().map(|&k| raw_coefficients(k, &mut rng)).collect();
let x_grid = linspace(-1.0, 1.0, n_pulse);
let mut qubits = Vec::with_capacity(n);
let mut x0 = Vec::new();
for q in 0..n {
let env = envelope(&p.amplitude_envelope[q], &x_grid, p.amplitude_order[q])?;
let qb = qubit_basis(bases[q].as_ref(), &env, p.n_para[q], p.wf_mode[q], n_pulse, &mut rng)?;
x0.extend(initial_params(bases[q].as_ref(), &raw[q], p.wf_mode[q]));
qubits.push(qb);
}
let rabi_max = scaled(&p.omega_r_max);
let rabi = rabi_instances(
&rabi_max,
&scaled(&p.sigma_omega_r_max),
cfg.optimization.omega_r_snapshots,
&mut rng,
);
let delta = per_qubit(&p.delta);
let sigma_delta = per_qubit(&p.sigma_delta);
let sw = scaled(&p.sw);
let specs: Vec<QubitOffsets> = (0..n)
.map(|q| QubitOffsets {
coverage: p.coverage[q],
delta: delta[q],
sigma: sigma_delta[q],
sw: sw[q],
bandwidth: two_pi * p.pulse_bandwidth[q],
ratio: p.ratio_factor[q],
profile_order: p.profile_order[q],
})
.collect();
let coupling = match (&p.j, n > 1) {
(Some(j), true) => Some(upper_coupling(j, n).map_err(Error::Config)?.map(|v| two_pi * v)),
_ => None,
};
let sigma_j = two_pi * p.sigma_j.unwrap_or(0.0);
let fixed = p.coverage.iter().all(|&c| c == Coverage::Single)
&& sigma_delta.iter().all(|&s| s == 0.0)
&& (coupling.is_none() || sigma_j == 0.0);
let per_qubit_offsets: Vec<Vec<f64>> = if fixed {
delta.iter().map(|&d| vec![d]).collect()
} else {
specs
.iter()
.map(|s| sample_offsets(s, cfg.optimization.h0_snapshots, &mut rng))
.collect()
};
let m = per_qubit_offsets[0].len();
let profiles: Vec<Vec<f64>> = specs
.iter()
.zip(&per_qubit_offsets)
.map(|(s, o)| excitation_profile(s, o))
.collect();
let couplings = coupling.as_ref().map(|j| coupling_instances(j, sigma_j, m, &mut rng));
let model = model_from_config(cfg)?;
let drifts = (0..m)
.map(|k| {
let offsets: Vec<f64> = per_qubit_offsets.iter().map(|o| o[k]).collect();
model.drift(&offsets, couplings.as_ref().map(|c| &c[k]))
})
.collect::<Result<Vec<_>>>()?;
let space = cfg.space();
let raw_state = |axes: &[String]| -> Result<CMat<f64>> {
match space {
Space::Hilbert => product_state(axes),
Space::Liouville => product_density(axes),
}
};
let embed = |s: &CMat<f64>| -> Result<CMat<f64>> {
match space {
Space::Hilbert => model.embed_state(s),
Space::Liouville => model.embed_density(s),
}
};
let apply = |u: &CMat<f64>, s: &CMat<f64>| -> Result<CMat<f64>> {
match space {
Space::Hilbert => u.matmul(s),
Space::Liouville => u.matmul(s)?.matmul(&u.adjoint()),
}
};
let raw_initials: Vec<CMat<f64>> = cfg.initial_states.iter().map(|a| raw_state(a)).collect::<Result<_>>()?;
let initial_states: Vec<CMat<f64>> = raw_initials.iter().map(&embed).collect::<Result<_>>()?;
let in_band = |q: usize, k: usize| profiles[q][k] == 1.0;
let everyone_in_band = |k: usize| (0..n).all(|q| in_band(q, k));
let rows: Vec<Row> = match cfg.target_states.single_gate() {
Some(name) => {
let v = gate(name, n)?;
let id = CMat::<f64>::identity(1 << n);
let gates: Vec<&CMat<f64>> = (0..m).map(|k| if everyone_in_band(k) { &v } else { &id }).collect();
gate_rows(&gates, space, model.as_ref())?
}
None => {
let deg = std::f64::consts::PI / 180.0;
let target = |pi: usize, init: &CMat<f64>, k: usize| -> Result<CMat<f64>> {
let t = match &cfg.target_states {
Targets::Axis(axes) => {
let per_qubit: Vec<String> = (0..n)
.map(|q| {
if in_band(q, k) {
&axes[pi][q]
} else {
&cfg.initial_states[pi][q]
}
})
.cloned()
.collect();
raw_state(&per_qubit)?
}
Targets::Gate(names) if everyone_in_band(k) => apply(&gate(&names[pi], n)?, init)?,
Targets::Gate(_) => init.clone(),
Targets::PhiBeta { phi, beta } => {
let angles: Vec<f64> = (0..n).map(|q| beta[pi][q] * deg * profiles[q][k]).collect();
apply(&rotation(&phi[pi], &angles, n)?, init)?
}
};
let t = embed(&t)?;
if space == Space::Liouville && (t.matmul(&t)?.trace().re - 1.0).abs() > 1e-9 {
return Err(Error::NotSupported(
"Liouville-space fidelity needs pure targets; this target is mixed".into(),
));
}
Ok(t)
};
raw_initials
.iter()
.enumerate()
.map(|(pi, init)| {
Ok(Row {
evolved: embed(init)?,
targets: (0..m).map(|k| target(pi, init, k)).collect::<Result<_>>()?,
})
})
.collect::<Result<_>>()?
}
};
let mut batch = Vec::with_capacity(rows.len() * m * rabi.len());
for (pi, row) in rows.into_iter().enumerate() {
for (k, h0) in drifts.iter().enumerate() {
for (r, rb) in rabi.iter().enumerate() {
batch.push(BatchElement {
h0: h0.clone(),
rabi: rb.clone(),
initial: row.evolved.clone(),
target: row.targets[k].clone(),
initial_index: pi,
snapshot: k,
rabi_index: r,
});
}
}
}
let evolution = if cfg.is_dissipative() {
let (t1, t2) = (p.t1.as_deref().unwrap_or(&[]), p.t2.as_deref().unwrap_or(&[]));
Evolution::Lindblad(LindbladOps::new(&collapse_operators(t1, t2)?, dt)?)
} else if space == Space::Liouville {
Evolution::Liouville
} else {
Evolution::Hilbert
};
let offsets = scaled(&p.pulse_offset);
let modulation = CMat::from_fn(n_pulse, n, |t, q| {
C::from_polar(1.0, offsets[q] * (times[t] - duration / 2.0))
});
let observables = pauli_ops(n)
.iter()
.map(|o| -> Result<[CMat<f64>; 3]> {
Ok([
model.embed_density(&o[0])?,
model.embed_density(&o[1])?,
model.embed_density(&o[2])?,
])
})
.collect::<Result<Vec<_>>>()?;
Ok(Problem {
n_qubits: n,
dim: model.dim(),
n_pulse,
dt,
duration,
times,
qubits,
modulation,
control_ops: model.control_ops(),
channels: model.control_channels(),
batch,
evolution,
space,
x0,
seed,
target_fidelity: cfg.optimization.targ_fid,
max_iter: cfg.optimization.max_iter,
algorithm: cfg.optimization.algorithm.clone(),
initial_states,
observables,
model,
rabi_max,
delta,
sw,
coupling,
n_snapshots: m,
n_rabi: rabi.len(),
})
}
}
struct Row {
evolved: CMat<f64>,
targets: Vec<CMat<f64>>,
}
fn gate_rows(gates: &[&CMat<f64>], space: Space, model: &dyn HamiltonianModel) -> Result<Vec<Row>> {
let d = gates.first().map_or(1, |g| g.rows);
match space {
Space::Hilbert => Ok(vec![Row {
evolved: model.embed_state(&CMat::identity(d))?,
targets: gates.iter().map(|w| model.embed_state(w)).collect::<Result<_>>()?,
}]),
Space::Liouville => pauli_strings(d.trailing_zeros() as usize)
.iter()
.enumerate()
.map(|(j, u)| {
let weight = if j == 0 { 1.0 } else { 1.0 / (d + 1) as f64 };
let targets = gates
.iter()
.map(|w| {
Ok(model
.embed_density(&w.matmul(u)?.matmul(&w.adjoint())?)?
.scale_re(weight))
})
.collect::<Result<_>>()?;
Ok(Row {
evolved: model.embed_density(u)?,
targets,
})
})
.collect(),
}
}
fn pauli_strings(n: usize) -> Vec<CMat<f64>> {
let [x, y, z] = paulis();
let single = [CMat::identity(2), x, y, z];
(0..1usize << (2 * n))
.map(|i| {
(0..n)
.rev()
.fold(CMat::identity(1), |acc, q| acc.kron(&single[(i >> (2 * q)) & 3]))
})
.collect()
}
#[cfg(test)]
mod tests;