Skip to content
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions compiler/qsc_circuit/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ impl Backend for Builder {
self.push_gate(gate("S", [q]));
}

fn sx(&mut self, q: usize) {
let q = self.map(q);
self.push_gate(gate("SX", [q]));
}

fn swap(&mut self, q0: usize, q1: usize) {
let q0 = self.map(q0);
let q1 = self.map(q1);
Expand Down
31 changes: 2 additions & 29 deletions compiler/qsc_circuit/src/circuit_to_qsharp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,35 +203,8 @@ fn generate_unitary_call(
qubits: &FxHashMap<usize, String>,
indent: &str,
) -> String {
// "SX" will generate three operations: H, X and H
if unitary.gate == "SX" {
let h_str = operation_call(
&Unitary {
gate: "H".to_string(),
args: vec![],
children: vec![],
targets: unitary.targets.clone(),
controls: unitary.controls.clone(),
is_adjoint: false,
},
qubits,
);
let s_str = operation_call(
&Unitary {
gate: "S".to_string(),
args: vec![],
children: vec![],
targets: unitary.targets.clone(),
controls: unitary.controls.clone(),
is_adjoint: unitary.is_adjoint,
},
qubits,
);
format!("{indent}{h_str};\n{indent}{s_str};\n{indent}{h_str};\n")
} else {
let operation_str = operation_call(unitary, qubits);
format!("{indent}{operation_str};\n")
}
let operation_str = operation_call(unitary, qubits);
format!("{indent}{operation_str};\n")
}

fn generate_ket_call(ket: &Ket, qubits: &FxHashMap<usize, String>, indent: &str) -> String {
Expand Down
8 changes: 2 additions & 6 deletions compiler/qsc_circuit/src/circuit_to_qsharp/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -611,9 +611,7 @@ fn circuit_with_sqrt_x_gate() {
}
H(qs[0]);
Z(qs[0]);
H(qs[1]);
S(qs[1]);
H(qs[1]);
SX(qs[1]);
Z(qs[1]);
}

Expand Down Expand Up @@ -660,9 +658,7 @@ fn circuit_with_ctrl_adj_sqrt_x_gate() {
}
H(qs[0]);
Z(qs[0]);
Controlled H([qs[1]], qs[0]);
Controlled Adjoint S([qs[1]], qs[0]);
Controlled H([qs[1]], qs[0]);
Controlled Adjoint SX([qs[1]], qs[0]);
Z(qs[1]);
}

Expand Down
15 changes: 15 additions & 0 deletions compiler/qsc_eval/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ pub trait Backend {
fn s(&mut self, _q: usize) {
unimplemented!("s gate");
}
fn sx(&mut self, _q: usize) {
unimplemented!("sx gate");
}
fn swap(&mut self, _q0: usize, _q1: usize) {
unimplemented!("swap gate");
}
Expand Down Expand Up @@ -295,6 +298,13 @@ impl Backend for SparseSim {
self.apply_noise(q);
}

fn sx(&mut self, q: usize) {
self.sim.h(q);
self.sim.s(q);
self.sim.h(q);
self.apply_noise(q);
}

fn swap(&mut self, q0: usize, q1: usize) {
self.sim.swap_qubit_ids(q0, q1);
self.apply_noise(q0);
Expand Down Expand Up @@ -593,6 +603,11 @@ where
self.main.s(q);
}

fn sx(&mut self, q: usize) {
self.chained.sx(q);
self.main.sx(q);
}

fn swap(&mut self, q0: usize, q1: usize) {
self.chained.swap(q0, q1);
self.main.swap(q0, q1);
Expand Down
1 change: 1 addition & 0 deletions compiler/qsc_eval/src/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ pub(crate) fn call(
"__quantum__qis__h__body" => one_qubit_gate(|q| sim.h(q), arg, arg_span),
"__quantum__qis__s__body" => one_qubit_gate(|q| sim.s(q), arg, arg_span),
"__quantum__qis__s__adj" => one_qubit_gate(|q| sim.sadj(q), arg, arg_span),
"__quantum__qis__sx__body" => one_qubit_gate(|q| sim.sx(q), arg, arg_span),
"__quantum__qis__t__body" => one_qubit_gate(|q| sim.t(q), arg, arg_span),
"__quantum__qis__t__adj" => one_qubit_gate(|q| sim.tadj(q), arg, arg_span),
"__quantum__qis__x__body" => one_qubit_gate(|q| sim.x(q), arg, arg_span),
Expand Down
37 changes: 37 additions & 0 deletions compiler/qsc_eval/src/intrinsic/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ impl Backend for CustomSim {
self.sim.s(q);
}

fn sx(&mut self, q: usize) {
self.sim.h(q);
self.sim.s(q);
self.sim.h(q);
}

fn swap(&mut self, q0: usize, q1: usize) {
self.sim.swap(q0, q1);
}
Expand Down Expand Up @@ -1228,6 +1234,37 @@ fn sadj_qubit_already_released_fails() {
);
}

#[test]
fn sx() {
check_intrinsic_result(
"",
indoc! {r#"{
use q1 = Qubit();
QIR.Intrinsic.__quantum__qis__sx__body(q1);
if Microsoft.Quantum.Diagnostics.CheckZero(q1) {
fail "Qubit should be in one state.";
}
QIR.Intrinsic.__quantum__qis__sx__body(q1);
QIR.Intrinsic.__quantum__qis__sx__body(q1);
QIR.Intrinsic.__quantum__qis__sx__body(q1);
Microsoft.Quantum.Diagnostics.CheckZero(q1)
}"#},
&expect!["true"],
);
}

#[test]
fn sx_qubit_already_released_fails() {
check_intrinsic_result(
"",
indoc! {"{
let q = { use q = Qubit(); q };
QIR.Intrinsic.__quantum__qis__sx__body(q)
}"},
&expect!["qubit used after release"],
);
}

#[test]
fn t() {
check_intrinsic_result(
Expand Down
20 changes: 20 additions & 0 deletions compiler/qsc_partial_eval/src/tests/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,26 @@ fn call_to_intrinsic_adjoint_s_adds_callable_and_generates_instruction() {
);
}

#[test]
fn call_to_intrinsic_sx_adds_callable_and_generates_instruction() {
check_call_to_single_qubit_instrinsic_adds_callable_and_generates_instruction(
"__quantum__qis__sx__body",
&expect![[r#"
Callable:
name: __quantum__qis__sx__body
call_type: Regular
input_type:
[0]: Qubit
output_type: <VOID>
body: <NONE>"#]],
&expect![[r#"
Block:
Call id(1), args( Qubit(0), )
Call id(2), args( Integer(0), Pointer, )
Return"#]],
);
}

#[test]
fn call_to_intrinsic_t_adds_callable_and_generates_instruction() {
check_call_to_single_qubit_instrinsic_adds_callable_and_generates_instruction(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,7 @@ operation tdg(qubit : Qubit) : Unit is Adj + Ctl {
}

operation sx(qubit : Qubit) : Unit is Adj + Ctl {
Rx(Std.Math.PI() / 2., qubit);
Adjoint R(PauliI, Std.Math.PI() / 2., qubit);
SX(qubit);
}

operation rx(theta : __Angle__, qubit : Qubit) : Unit is Adj + Ctl {
Expand Down
25 changes: 25 additions & 0 deletions compiler/qsc_rca/src/tests/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,31 @@ fn check_rca_for_quantum_qis_s_adj() {
);
}

#[test]
fn check_rca_for_quantum_qis_sx_body() {
let compilation_context = CompilationContext::default();
check_callable_compute_properties(
&compilation_context.fir_store,
compilation_context.get_compute_properties(),
"__quantum__qis__sx__body",
&expect![
r#"
Callable: CallableComputeProperties:
body: ApplicationsGeneratorSet:
inherent: Quantum: QuantumProperties:
runtime_features: RuntimeFeatureFlags(0x0)
value_kind: Element(Static)
dynamic_param_applications:
[0]: [Parameter Type Element] Quantum: QuantumProperties:
runtime_features: RuntimeFeatureFlags(UseOfDynamicQubit)
value_kind: Element(Static)
adj: <none>
ctl: <none>
ctl-adj: <none>"#
],
);
}

#[test]
fn check_rca_for_quantum_qis_t_body() {
let compilation_context = CompilationContext::default();
Expand Down
Loading
Loading