Skip to content

Commit

Permalink
Add QrackCircuit inverse() and past_light_cone()
Browse files Browse the repository at this point in the history
  • Loading branch information
WrathfulSpatula committed Sep 10, 2023
1 parent e2706a3 commit 7bc117c
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 28 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "qook"
version = "0.8.0"
version = "0.9.0"
license = "MIT"
description = "qook - Pure Rust unitaryfund/qrack Wrapper"
documentation = "https://pyqrack.readthedocs.io/en/latest/"
Expand Down
32 changes: 28 additions & 4 deletions include/pinvoke_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ struct _QrackTimeEvolveOpHeader;
extern "C" {
// non-quantum
MICROSOFT_QUANTUM_DECL int get_error(_In_ uintq sid);
MICROSOFT_QUANTUM_DECL uintq init_count_type(_In_ uintq q, _In_ bool md, _In_ bool sd, _In_ bool sh, _In_ bool bdt,
_In_ bool pg, _In_ bool zxf, _In_ bool hy, _In_ bool oc, _In_ bool dm);
MICROSOFT_QUANTUM_DECL uintq init_count_type(_In_ uintq q, _In_ bool tn, _In_ bool md, _In_ bool sd, _In_ bool sh,
_In_ bool bdt, _In_ bool pg, _In_ bool zxf, _In_ bool hy, _In_ bool oc, _In_ bool dm);
MICROSOFT_QUANTUM_DECL uintq init_count(_In_ uintq q, _In_ bool dm);
MICROSOFT_QUANTUM_DECL uintq init_count_pager(_In_ uintq q, _In_ bool dm);
MICROSOFT_QUANTUM_DECL uintq init() { return init_count(0, false); }
Expand All @@ -51,9 +51,32 @@ MICROSOFT_QUANTUM_DECL void qstabilizer_in_from_file(_In_ uintq sid, _In_ char*

// pseudo-quantum
MICROSOFT_QUANTUM_DECL double Prob(_In_ uintq sid, _In_ uintq q);
MICROSOFT_QUANTUM_DECL double ProbRdm(_In_ uintq sid, _In_ uintq q);
MICROSOFT_QUANTUM_DECL double PermutationProb(
_In_ uintq sid, _In_ uintq n, _In_reads_(n) uintq* q, _In_reads_(n) bool* c);
MICROSOFT_QUANTUM_DECL double PermutationExpectation(_In_ uintq sid, _In_ uintq n, _In_reads_(n) uintq* c);
MICROSOFT_QUANTUM_DECL double PermutationProbRdm(
_In_ uintq sid, _In_ uintq n, _In_reads_(n) uintq* q, _In_reads_(n) bool* c, _In_ bool r);
MICROSOFT_QUANTUM_DECL double PermutationExpectation(_In_ uintq sid, _In_ uintq n, _In_reads_(n) uintq* q);
MICROSOFT_QUANTUM_DECL double PermutationExpectationRdm(
_In_ uintq sid, _In_ uintq n, _In_reads_(n) uintq* q, _In_ bool r);
MICROSOFT_QUANTUM_DECL double FactorizedExpectation(
_In_ uintq sid, _In_ uintq n, _In_reads_(n) uintq* q, _In_ uintq m, uintq* c);
MICROSOFT_QUANTUM_DECL double FactorizedExpectationRdm(
_In_ uintq sid, _In_ uintq n, _In_reads_(n) uintq* q, _In_ uintq m, uintq* c, _In_ bool r);
#if FPPOW < 6
MICROSOFT_QUANTUM_DECL double FactorizedExpectationFp(_In_ uintq sid, _In_ uintq n, _In_reads_(n) uintq* q, float* c);
MICROSOFT_QUANTUM_DECL double FactorizedExpectationFpRdm(
_In_ uintq sid, _In_ uintq n, _In_reads_(n) uintq* q, float* c, _In_ bool r);
#elif FPPOW < 7
MICROSOFT_QUANTUM_DECL double FactorizedExpectationFp(_In_ uintq sid, _In_ uintq n, _In_reads_(n) uintq* q, double* c);
MICROSOFT_QUANTUM_DECL double FactorizedExpectationFpRdm(
_In_ uintq sid, _In_ uintq n, _In_reads_(n) uintq* q, double* c, _In_ bool r);
#else
MICROSOFT_QUANTUM_DECL double FactorizedExpectationFp(
_In_ uintq sid, _In_ uintq n, _In_reads_(n) uintq* q, boost::multiprecision::float128* c);
MICROSOFT_QUANTUM_DECL double FactorizedExpectationFpRdm(
_In_ uintq sid, _In_ uintq n, _In_reads_(n) uintq* q, boost::multiprecision::float128* c, _In_ bool r);
#endif

MICROSOFT_QUANTUM_DECL void DumpIds(_In_ uintq sid, _In_ IdCallback callback);
MICROSOFT_QUANTUM_DECL void Dump(_In_ uintq sid, _In_ ProbAmpCallback callback);
Expand Down Expand Up @@ -238,7 +261,6 @@ MICROSOFT_QUANTUM_DECL void ResetUnitaryFidelity(_In_ uintq sid);
MICROSOFT_QUANTUM_DECL void SetSdrp(_In_ uintq sid, _In_ double sdrp);
MICROSOFT_QUANTUM_DECL void SetReactiveSeparate(_In_ uintq sid, _In_ bool irs);
MICROSOFT_QUANTUM_DECL void SetTInjection(_In_ uintq sid, _In_ bool iti);
MICROSOFT_QUANTUM_DECL void SetStabilizerWeakSampling(_In_ uintq sid, _In_ bool sws);

#if !(FPPOW < 6 && !ENABLE_COMPLEX_X2)
MICROSOFT_QUANTUM_DECL void TimeEvolve(_In_ uintq sid, _In_ double t, _In_ uintq n,
Expand Down Expand Up @@ -274,6 +296,8 @@ MICROSOFT_QUANTUM_DECL void qneuron_learn_permutation(_In_ uintq nid, _In_ doubl

MICROSOFT_QUANTUM_DECL uintq init_qcircuit(_In_ bool collapse);
MICROSOFT_QUANTUM_DECL uintq init_qcircuit_clone(_In_ uintq cid);
MICROSOFT_QUANTUM_DECL uintq qcircuit_inverse(_In_ uintq cid);
MICROSOFT_QUANTUM_DECL uintq qcircuit_past_light_cone(_In_ uintq cid, _In_ uintq n, _In_reads_(n) uintq* q);
MICROSOFT_QUANTUM_DECL void destroy_qcircuit(_In_ uintq cid);
MICROSOFT_QUANTUM_DECL uintq get_qcircuit_qubit_count(_In_ uintq cid);
MICROSOFT_QUANTUM_DECL void qcircuit_swap(_In_ uintq cid, _In_ uintq q1, _In_ uintq q2);
Expand Down
21 changes: 21 additions & 0 deletions src/qrack_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,27 @@ impl QrackCircuit {
}
}

pub fn inverse(&self) -> QrackCircuit {
let cid;
unsafe {
cid = qrack_system::qcircuit_inverse(self.cid);
}
Self{
cid
}
}

pub fn past_light_cone(&self, q: Vec<u64>) -> QrackCircuit {
let cid;
let mut _q = q.to_vec();
unsafe {
cid = qrack_system::qcircuit_past_light_cone(self.cid, _q.len() as u64, _q.as_mut_ptr());
}
Self{
cid
}
}

pub fn swap(&self, q1: u64, q2: u64) -> () {
// Add a 'Swap' gate to the circuit
//
Expand Down
23 changes: 4 additions & 19 deletions src/qrack_simulator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ impl QrackSimulator {
return Ok(Self{ sid });
}
pub fn new_layers(qubit_count: u64,
is_tensor_network: bool,
is_schmidt_decompose_multi: bool,
is_schmidt_decompose: bool,
is_stabilizer_hybrid: bool,
Expand All @@ -56,7 +57,8 @@ impl QrackSimulator {
is_host_pointer: bool) -> Result<Self, QrackError> {

let sid;
if is_schmidt_decompose
if is_tensor_network
&& is_schmidt_decompose
&& is_stabilizer_hybrid
&& !is_binary_decision_tree
&& is_paged
Expand All @@ -74,6 +76,7 @@ impl QrackSimulator {
} else {
unsafe {
sid = qrack_system::init_count_type(qubit_count,
is_tensor_network,
is_schmidt_decompose_multi,
is_schmidt_decompose,
is_stabilizer_hybrid,
Expand Down Expand Up @@ -2469,24 +2472,6 @@ impl QrackSimulator {
self.check_error()
}

pub fn set_weak_sampling(&self, sws: bool) -> Result<(), QrackError> {
// Set reactive separation option
//
// If reactive separation is available, then this method turns it off/on.
// Note that reactive separation is on by default.
//
// Args:
// irs(bool): on/off for aggressive separation
//
// Raises:
// RuntimeError: QrackSimulator raised an exception.

unsafe {
qrack_system::SetStabilizerWeakSampling(self.sid, sws);
}
self.check_error()
}

pub fn set_t_injection(self, iti: bool) -> Result<(), QrackError> {
// Set t-injection option
//
Expand Down
52 changes: 48 additions & 4 deletions src/qrack_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ extern "C" {
extern "C" {
pub fn init_count_type(
q: uintq,
tn: bool,
md: bool,
sd: bool,
sh: bool,
Expand Down Expand Up @@ -119,11 +120,51 @@ extern "C" {
extern "C" {
pub fn Prob(sid: uintq, q: uintq) -> f64;
}
extern "C" {
pub fn ProbRdm(sid: uintq, q: uintq) -> f64;
}
extern "C" {
pub fn PermutationProb(sid: uintq, n: uintq, q: *mut uintq, c: *mut bool) -> f64;
}
extern "C" {
pub fn PermutationExpectation(sid: uintq, n: uintq, c: *mut uintq) -> f64;
pub fn PermutationProbRdm(sid: uintq, n: uintq, q: *mut uintq, c: *mut bool, r: bool) -> f64;
}
extern "C" {
pub fn PermutationExpectation(sid: uintq, n: uintq, q: *mut uintq) -> f64;
}
extern "C" {
pub fn PermutationExpectationRdm(sid: uintq, n: uintq, q: *mut uintq, r: bool) -> f64;
}
extern "C" {
pub fn FactorizedExpectation(
sid: uintq,
n: uintq,
q: *mut uintq,
m: uintq,
c: *mut uintq,
) -> f64;
}
extern "C" {
pub fn FactorizedExpectationRdm(
sid: uintq,
n: uintq,
q: *mut uintq,
m: uintq,
c: *mut uintq,
r: bool,
) -> f64;
}
extern "C" {
pub fn FactorizedExpectationFp(sid: uintq, n: uintq, q: *mut uintq, c: *mut f32) -> f64;
}
extern "C" {
pub fn FactorizedExpectationFpRdm(
sid: uintq,
n: uintq,
q: *mut uintq,
c: *mut f32,
r: bool,
) -> f64;
}
extern "C" {
pub fn DumpIds(sid: uintq, callback: IdCallback);
Expand Down Expand Up @@ -567,9 +608,6 @@ extern "C" {
extern "C" {
pub fn SetTInjection(sid: uintq, iti: bool);
}
extern "C" {
pub fn SetStabilizerWeakSampling(sid: uintq, sws: bool);
}
extern "C" {
pub fn TimeEvolve(
sid: uintq,
Expand Down Expand Up @@ -636,6 +674,12 @@ extern "C" {
extern "C" {
pub fn init_qcircuit_clone(cid: uintq) -> uintq;
}
extern "C" {
pub fn qcircuit_inverse(cid: uintq) -> uintq;
}
extern "C" {
pub fn qcircuit_past_light_cone(cid: uintq, n: uintq, q: *mut uintq) -> uintq;
}
extern "C" {
pub fn destroy_qcircuit(cid: uintq);
}
Expand Down

0 comments on commit 7bc117c

Please sign in to comment.