Skip to content

Commit

Permalink
PragmaLoop 3.0Braket bugfix (#96)
Browse files Browse the repository at this point in the history
* PragmaLoop for 3.0Braket bugfix

* Changed test
  • Loading branch information
kbarkhqs authored Aug 7, 2023
1 parent 9fb2fd9 commit 93b6390
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 38 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

This changelog track changes to the qoqo qasm project starting at version 0.5.0

### 0.8.3

* Bugfix for PragmaLoop in 3.0Braket and 2.0

### 0.8.2

* Bugfix for PragmaGlobalPhase in 3.0Braket
Expand Down
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions qoqo_qasm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "qoqo_qasm"
version = "0.8.2"
version = "0.8.3"
authors = ["HQS Quantum Simulations <info@quantumsimulations.de>"]
license = "Apache-2.0"
edition = "2018"
Expand Down Expand Up @@ -29,7 +29,7 @@ crate-type = ["cdylib", "rlib"]
[dependencies]
qoqo = { version = "1.5.1", default-features = false }
roqoqo = { version = "1.5.1", features = ["serialize"] }
roqoqo-qasm = { version = "0.8", path = "../roqoqo-qasm" }
roqoqo-qasm = { version = "0.8.3", path = "../roqoqo-qasm" }

[dependencies.pyo3]
version = "0.19"
Expand Down
2 changes: 1 addition & 1 deletion qoqo_qasm/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "qoqo_qasm"
version = "0.8.2"
version = "0.8.3"
dependencies = [
'qoqo>=1.5',
'qoqo_calculator_pyo3>=1.1',
Expand Down
4 changes: 2 additions & 2 deletions qoqo_qasm/qoqo_qasm/DEPENDENCIES
Original file line number Diff line number Diff line change
Expand Up @@ -9279,7 +9279,7 @@ LICENSE:


====================================================
qoqo_qasm 0.8.2
qoqo_qasm 0.8.3
https://github.com/HQSquantumsimulations/qoqo_qasm
by HQS Quantum Simulations <info@quantumsimulations.de>
Python interface of roqoqo mocked backend
Expand Down Expand Up @@ -11324,7 +11324,7 @@ LICENSE:


====================================================
roqoqo-qasm 0.8.2
roqoqo-qasm 0.8.3
https://github.com/HQSquantumsimulations/qoqo_qasm
by HQS Quantum Simulations <info@quantumsimulations.de>
QASM interface for roqoqo rust quantum computing toolkit
Expand Down
31 changes: 31 additions & 0 deletions qoqo_qasm/tests/integration/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,37 @@ fn test_call_operation_different_2_roqoqo_3(
})
}

#[test_case(Operation::from(PragmaLoop::new(2.0.into(), Circuit::new() + PauliX::new(0))), "pragma roqoqo PragmaLoop 2e0 PauliX(PauliX { qubit: 0 })\n;", "for uint i in [0:2] {\n x q[0];\n}", "x q[0];\nx q[0];\n"; "PragmaLoop")]
fn test_call_operation_error_different_all(
operation: Operation,
converted_3_roqoqo: &str,
converted_3_vanilla: &str,
converted_2_converted_3_braket: &str,
) {
pyo3::prepare_freethreaded_python();
Python::with_gil(|py| {
let new_op: Py<PyAny> = convert_operation_to_pyobject(operation.clone()).unwrap();

assert_eq!(
qasm_call_operation(new_op.as_ref(py), "q", "3.0Braket").unwrap(),
converted_2_converted_3_braket.to_string()
);
assert_eq!(
qasm_call_operation(new_op.as_ref(py), "q", "2.0").unwrap(),
converted_2_converted_3_braket.to_string()
);

assert_eq!(
qasm_call_operation(new_op.as_ref(py), "q", "3.0Roqoqo").unwrap(),
converted_3_roqoqo.to_string()
);
assert_eq!(
qasm_call_operation(new_op.as_ref(py), "q", "3.0").unwrap(),
converted_3_vanilla.to_string()
);
})
}

#[test_case(Operation::from(PragmaBoostNoise::new(1.5.into())), "pragma roqoqo PragmaBoostNoise 1.5e0;"; "PragmaBoostNoise")]
#[test_case(Operation::from(PragmaDamping::new(0, 1.0.into(), 1.5.into())), "pragma roqoqo PragmaDamping 0 1e0 1.5e0;"; "PragmaDamping")]
#[test_case(Operation::from(PragmaDephasing::new(0, 1.0.into(), 1.5.into())), "pragma roqoqo PragmaDephasing 0 1e0 1.5e0;"; "PragmaDephasing")]
Expand Down
2 changes: 1 addition & 1 deletion roqoqo-qasm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "roqoqo-qasm"
version = "0.8.2"
version = "0.8.3"
authors = ["HQS Quantum Simulations <info@quantumsimulations.de>"]
license = "Apache-2.0"
edition = "2018"
Expand Down
27 changes: 22 additions & 5 deletions roqoqo-qasm/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const ALLOWED_OPERATIONS: &[&str; 11] = &[
];

// Operations that are ignored when looking for a QASM definition
const NO_DEFINITION_REQUIRED_OPERATIONS: &[&str; 10] = &[
const NO_DEFINITION_REQUIRED_OPERATIONS: &[&str; 11] = &[
"SingleQubitGate",
"DefinitionFloat",
"DefinitionUsize",
Expand All @@ -47,6 +47,7 @@ const NO_DEFINITION_REQUIRED_OPERATIONS: &[&str; 10] = &[
"PragmaGlobalPhase",
"PragmaRepeatedMeasurement",
"MeasureQubit",
"PragmaLoop",
];

/// Translate the qoqo circuit into QASM ouput.
Expand Down Expand Up @@ -673,16 +674,13 @@ pub fn call_operation(
}
},
Operation::PragmaLoop(op) => match qasm_version {
QasmVersion::V2point0 => Err(RoqoqoBackendError::GenericError {
msg: "PragmaLoop not allowed with qasm_version 2.0".to_string(),
}),
QasmVersion::V3point0(Qasm3Dialect::Roqoqo) => Ok(format!(
"pragma roqoqo {} {} {};",
op.hqslang(),
op.repetitions(),
op.circuit()
)),
QasmVersion::V3point0(_) => {
QasmVersion::V3point0(Qasm3Dialect::Vanilla) => {
let mut data = "".to_string();
match op.repetitions() {
CalculatorFloat::Float(x) => {
Expand All @@ -700,6 +698,25 @@ pub fn call_operation(
CalculatorFloat::Str(x) => Err(RoqoqoBackendError::GenericError { msg: format!("Used PragmaLoop with a string {x} for repetitions and a qasm-version that is incompatible: {qasm_version:?}") })
}
}
_ => {
let mut data = "".to_string();
match op.repetitions() {
CalculatorFloat::Float(x) => {
for _ in 0_usize..(*x as usize) {
let circuit_vec = match call_circuit(op.circuit(), qubit_register_name, qasm_version) {
Ok(vec_str) => vec_str,
Err(x) => return Err(x)
};
for string in circuit_vec {
data.push_str(string.as_str());
data.push('\n');
}
}
Ok(data)
},
CalculatorFloat::Str(x) => Err(RoqoqoBackendError::GenericError { msg: format!("Used PragmaLoop with a string {x} for repetitions and a qasm-version that is incompatible: {qasm_version:?}") })
}
}
},
Operation::PragmaOverrotation(op) => match qasm_version {
QasmVersion::V3point0(Qasm3Dialect::Roqoqo) => Ok(format!(
Expand Down
50 changes: 25 additions & 25 deletions roqoqo-qasm/tests/integration/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -479,16 +479,23 @@ fn test_pragma_loop() {
let mut circuit = Circuit::new();
circuit += Hadamard::new(0);

let pcond = PragmaLoop::new("test".into(), circuit.clone());

let pcond = PragmaLoop::new(2.0.into(), circuit.clone());
let data_3 = "h q[0];\nh q[0];\n";
assert_eq!(
call_operation(&Operation::from(pcond.clone()), "q", QasmVersion::V2point0),
Err(RoqoqoBackendError::GenericError {
msg: "PragmaLoop not allowed with qasm_version 2.0".into()
})
call_operation(&Operation::from(pcond.clone()), "q", QasmVersion::V2point0).unwrap(),
data_3
);
assert_eq!(
call_operation(
&Operation::from(pcond.clone()),
"q",
QasmVersion::V3point0(Qasm3Dialect::Braket)
)
.unwrap(),
data_3
);

let data_3_roqoqo = "pragma roqoqo PragmaLoop test Hadamard(Hadamard { qubit: 0 })\n;";
let data_3_roqoqo = "pragma roqoqo PragmaLoop 2e0 Hadamard(Hadamard { qubit: 0 })\n;";
assert_eq!(
call_operation(
&Operation::from(pcond),
Expand All @@ -499,34 +506,27 @@ fn test_pragma_loop() {
data_3_roqoqo
);

let pcond = PragmaLoop::new("test".into(), circuit.clone());
let qasm = QasmVersion::V3point0(Qasm3Dialect::Vanilla);
let pcond_error = PragmaLoop::new("test".into(), circuit.clone());
assert_eq!(
call_operation(&Operation::from(pcond.clone()), "q", qasm),
Err(RoqoqoBackendError::GenericError { msg: format!("Used PragmaLoop with a string test for repetitions and a qasm-version that is incompatible: {qasm:?}") })
call_operation(&Operation::from(pcond_error.clone()), "q", QasmVersion::V3point0(Qasm3Dialect::Vanilla)),
Err(RoqoqoBackendError::GenericError { msg: "Used PragmaLoop with a string test for repetitions and a qasm-version that is incompatible: V3point0(Vanilla)".into() })
);
let qasm = QasmVersion::V3point0(Qasm3Dialect::Braket);
assert_eq!(
call_operation(&Operation::from(pcond), "q", qasm),
Err(RoqoqoBackendError::GenericError { msg: format!("Used PragmaLoop with a string test for repetitions and a qasm-version that is incompatible: {qasm:?}") })
call_operation(&Operation::from(pcond_error.clone()), "q", QasmVersion::V3point0(Qasm3Dialect::Braket)),
Err(RoqoqoBackendError::GenericError { msg: "Used PragmaLoop with a string test for repetitions and a qasm-version that is incompatible: V3point0(Braket)".into() })
);
assert_eq!(
call_operation(&Operation::from(pcond_error), "q", QasmVersion::V2point0),
Err(RoqoqoBackendError::GenericError { msg: "Used PragmaLoop with a string test for repetitions and a qasm-version that is incompatible: V2point0".into() })
);

let pcond = PragmaLoop::new(2.0.into(), circuit);
let data_3 = "for uint i in [0:2] {\n h q[0];\n}";
assert_eq!(
call_operation(
&Operation::from(pcond.clone()),
"q",
QasmVersion::V3point0(Qasm3Dialect::Vanilla)
)
.unwrap(),
data_3
);
assert_eq!(
call_operation(
&Operation::from(pcond),
"q",
QasmVersion::V3point0(Qasm3Dialect::Braket)
QasmVersion::V3point0(Qasm3Dialect::Vanilla)
)
.unwrap(),
data_3
Expand All @@ -543,7 +543,7 @@ fn test_pragma_loop() {
call_operation(
&Operation::from(pcond),
"q",
QasmVersion::V3point0(Qasm3Dialect::Braket)
QasmVersion::V3point0(Qasm3Dialect::Vanilla)
),
Err(error)
);
Expand Down

0 comments on commit 93b6390

Please sign in to comment.