Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix phase gate matrix, test utility #156

Merged
merged 5 commits into from
Apr 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions sparsesim/src/matrix_testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ pub fn rz(theta: f64) -> Array2<Complex64> {
pub fn g(theta: f64) -> Array2<Complex64> {
let neg_exp_theta = Complex64::exp(Complex64::new(0.0, -theta / 2.0));
array![
[neg_exp_theta, Complex64::zero()],
[Complex64::one(), Complex64::zero()],
[Complex64::zero(), neg_exp_theta]
]
}
Expand Down Expand Up @@ -385,36 +385,37 @@ mod tests {
{
let mut sim = QuantumSim::default();

// Allocte the control we use to verify behavior.
let ctl = sim.allocate();
sim.h(ctl);

// Allocate the controls we use to verify behavior.
// Allocate the requested number of targets, entangling the control with them.
let mut ctls = vec![];
let mut qs = vec![];
for _ in 0..count {
let ctl = sim.allocate();
let q = sim.allocate();
sim.h(ctl);
sim.mcx(&[ctl], q);
qs.push(q);
ctls.push(ctl);
}

op(&mut sim, &qs);
reference(&mut sim, &qs);

// Undo the entanglement.
for q in &qs {
sim.mcx(&[ctl], *q);
for (q, ctl) in qs.iter().zip(&ctls) {
sim.mcx(&[*ctl], *q);
sim.h(*ctl);
}
sim.h(ctl);

sim.dump();

// We know the operations are equal if the qubits are left in the zero state.
assert!(sim.joint_probability(&[ctl]).is_nearly_zero());
for q in qs {
assert!(sim.joint_probability(&[q]).is_nearly_zero());
for (q, ctl) in qs.iter().zip(&ctls) {
assert!(sim.joint_probability(&[*q]).is_nearly_zero());
assert!(sim.joint_probability(&[*ctl]).is_nearly_zero());
}

// Sparse state vector should have one entry for |0⟩.
// Dump the state first to force a flush of any queued operations.
sim.dump();
assert_eq!(sim.state.len(), 1);
}

Expand Down
Loading