diff --git a/crates/cqlib-core/src/ir/qasm2/dump.rs b/crates/cqlib-core/src/ir/qasm2/dump.rs index a1cffb1..0af0938 100644 --- a/crates/cqlib-core/src/ir/qasm2/dump.rs +++ b/crates/cqlib-core/src/ir/qasm2/dump.rs @@ -69,7 +69,7 @@ //! loading the generated OpenQASM 2.0 does not restore that phase. use crate::circuit::gate::{ - CircuitGate, ClassicalDataOp, Directive, Instruction, StandardGate, UnitaryGate, + CircuitGate, ClassicalDataOp, Directive, Instruction, MCGate, StandardGate, UnitaryGate, }; use crate::circuit::operation::Operation; use crate::circuit::parameter::Parameter; @@ -260,6 +260,9 @@ pub fn to_path>(circuit: &Circuit, path: P) -> Result<(), QasmDum } /// Dumps a circuit to a string in OpenQASM 2.0 format. +/// +/// Top-level qubits are mapped densely in ascending numeric identifier order, +/// matching [`Circuit::to_matrix`] with its default qubit order. pub fn dumps(circuit: &Circuit) -> Result { let mut output = String::new(); writeln!( @@ -285,21 +288,16 @@ pub fn dumps(circuit: &Circuit) -> Result { writeln!(&mut output)?; - // Pass 0: Collect and output used extended standard gate definitions - let mut used_standard_gates = HashSet::new(); - collect_used_standard_gates(circuit, &mut used_standard_gates); + // Pass 0: Collect actual output names, including those emitted by MCGate. + let mut used_gate_names = HashSet::new(); + collect_used_gate_names(circuit, &mut used_gate_names); let extended_gates = [ - StandardGate::CRX, - StandardGate::CRY, - StandardGate::RZZ, - StandardGate::RXX, - StandardGate::RYY, - StandardGate::RZX, + "crx", "cry", "rzz", "rxx", "ryy", "rzx", "swap", "fsim", "cswap", ]; for gate in extended_gates { - if used_standard_gates.contains(&gate) + if used_gate_names.contains(gate) && let Some(def) = get_extended_gate_definition(gate) { writeln!(&mut output, "{}", def)?; @@ -322,6 +320,19 @@ pub fn dumps(circuit: &Circuit) -> Result { &mut has_delay, )?; + // A helper definition and a user definition cannot share a QASM name. + for name in extended_gates { + if used_gate_names.contains(name) + && (defined_gates.contains_key(name) + || unitary_gate_defs.contains_key(name) + || opaque_unitary_gates.contains_key(name)) + { + return Err(QasmDumpError::FormatError(format!( + "Gate name '{name}' conflicts with a required OpenQASM 2.0 extension definition" + ))); + } + } + // Output CircuitGate definitions for gate in defined_gates.values() { dump_gate_definition(gate, &mut output)?; @@ -347,10 +358,13 @@ pub fn dumps(circuit: &Circuit) -> Result { } // Pass 2: Main circuit - // Map Qubit(i) -> "q[i]" + // Numeric identifiers need not be contiguous or match storage order. + // Preserve the default matrix basis while keeping the register width N. let mut qubit_map: HashMap = HashMap::new(); - for q in circuit.qubits() { - qubit_map.insert(q, format!("q[{}]", q.index())); + let mut qubits = circuit.qubits(); + qubits.sort_unstable(); + for (index, q) in qubits.into_iter().enumerate() { + qubit_map.insert(q, format!("q[{index}]")); } // Top-level parameter resolution is direct (Parameter objects are used as is) @@ -1193,8 +1207,25 @@ fn dump_directive( Ok(()) } +fn qasm2_mc_gate_name(mc_gate: &MCGate) -> Option<&'static str> { + match (mc_gate.num_ctrl_qubits(), mc_gate.base_gate()) { + (1, StandardGate::X) => Some("cx"), + (1, StandardGate::Z) => Some("cz"), + (1, StandardGate::Y) => Some("cy"), + (1, StandardGate::H) => Some("ch"), + (1, StandardGate::RX) => Some("crx"), + (1, StandardGate::RY) => Some("cry"), + (1, StandardGate::RZ) => Some("crz"), + (1, StandardGate::Phase) => Some("cu1"), + (1, StandardGate::CX) => Some("ccx"), + (1, StandardGate::SWAP) => Some("cswap"), + (2, StandardGate::X) => Some("ccx"), + _ => None, + } +} + fn dump_mc_gate( - mc_gate: &crate::circuit::gate::MCGate, + mc_gate: &MCGate, op: &Operation, circuit: &Circuit, output: &mut String, @@ -1204,25 +1235,12 @@ fn dump_mc_gate( let num_ctrl = mc_gate.num_ctrl_qubits(); let base = mc_gate.base_gate(); - let name = match (num_ctrl, base) { - (1, StandardGate::X) => "cx", - (1, StandardGate::Z) => "cz", - (1, StandardGate::Y) => "cy", - (1, StandardGate::H) => "ch", - (1, StandardGate::RX) => "crx", - (1, StandardGate::RY) => "cry", - (1, StandardGate::RZ) => "crz", - (1, StandardGate::Phase) => "cu1", - (1, StandardGate::CX) => "ccx", - (1, StandardGate::SWAP) => "cswap", - (2, StandardGate::X) => "ccx", - _ => { - return Err(QasmDumpError::FormatError(format!( - "Unsupported MCGate: {} controls on {}", - num_ctrl, base - ))); - } - }; + let name = qasm2_mc_gate_name(mc_gate).ok_or_else(|| { + QasmDumpError::FormatError(format!( + "Unsupported MCGate: {} controls on {}", + num_ctrl, base + )) + })?; let params_str = format_params(op, circuit, param_map)?; let mapped_qs = map_qubits(op, qubit_map); @@ -1231,58 +1249,58 @@ fn dump_mc_gate( Ok(()) } -fn collect_used_standard_gates(circuit: &Circuit, used_gates: &mut HashSet) { - collect_used_standard_gates_from_operations(circuit.operations(), used_gates); +fn collect_used_gate_names(circuit: &Circuit, used_gates: &mut HashSet<&'static str>) { + collect_used_gate_names_from_operations(circuit.operations(), used_gates); } -fn collect_used_standard_gates_from_operations( +fn collect_used_gate_names_from_operations( operations: &[Operation], - used_gates: &mut HashSet, + used_gates: &mut HashSet<&'static str>, ) { for op in operations { match &op.instruction { Instruction::Standard(sg) => { - used_gates.insert(*sg); + used_gates.extend(qasm2_builtin_gate_name(*sg)); + } + Instruction::McGate(mc_gate) => { + used_gates.extend(qasm2_mc_gate_name(mc_gate)); } Instruction::CircuitGate(cg) => { - collect_used_standard_gates(&cg.circuit.circuit, used_gates); + collect_used_gate_names(&cg.circuit.circuit, used_gates); } Instruction::UnitaryGate(ug) => { if let Some(fc) = ug.circuit() { - collect_used_standard_gates(&fc.circuit, used_gates); + collect_used_gate_names(&fc.circuit, used_gates); } } Instruction::ClassicalControl(control) => match control { ClassicalControlOp::If(if_op) => { - collect_used_standard_gates_from_operations( + collect_used_gate_names_from_operations( if_op.then_body().operations(), used_gates, ); if let Some(body) = if_op.else_body() { - collect_used_standard_gates_from_operations(body.operations(), used_gates); + collect_used_gate_names_from_operations(body.operations(), used_gates); } } ClassicalControlOp::While(while_op) => { - collect_used_standard_gates_from_operations( + collect_used_gate_names_from_operations( while_op.body().operations(), used_gates, ); } ClassicalControlOp::For(for_op) => { - collect_used_standard_gates_from_operations( - for_op.body().operations(), - used_gates, - ); + collect_used_gate_names_from_operations(for_op.body().operations(), used_gates); } ClassicalControlOp::Switch(switch_op) => { for case in switch_op.cases() { - collect_used_standard_gates_from_operations( + collect_used_gate_names_from_operations( case.body().operations(), used_gates, ); } if let Some(body) = switch_op.default() { - collect_used_standard_gates_from_operations(body.operations(), used_gates); + collect_used_gate_names_from_operations(body.operations(), used_gates); } } ClassicalControlOp::Break | ClassicalControlOp::Continue => {} @@ -1505,25 +1523,12 @@ fn operation_to_qasm( Instruction::McGate(mc_gate) => { let num_ctrl = mc_gate.num_ctrl_qubits(); let base = mc_gate.base_gate(); - let name = match (num_ctrl, base) { - (1, StandardGate::X) => "cx", - (2, StandardGate::X) => "ccx", - (1, StandardGate::Z) => "cz", - (1, StandardGate::Y) => "cy", - (1, StandardGate::H) => "ch", - (1, StandardGate::CX) => "ccx", - (1, StandardGate::RX) => "crx", - (1, StandardGate::RY) => "cry", - (1, StandardGate::RZ) => "crz", - (1, StandardGate::SWAP) => "cswap", - (1, StandardGate::Phase) => "cu1", - _ => { - return Err(QasmDumpError::FormatError(format!( - "Unsupported multi-controlled gate: {}-controlled {:?}", - num_ctrl, base - ))); - } - }; + let name = qasm2_mc_gate_name(mc_gate).ok_or_else(|| { + QasmDumpError::FormatError(format!( + "Unsupported multi-controlled gate: {}-controlled {:?}", + num_ctrl, base + )) + })?; let params_str = format_params(op, circuit, param_map)?; let mapped_qs = map_qubits(op, qubit_map); Ok(format!("{}{} {}", name, params_str, mapped_qs.join(","))) @@ -1565,22 +1570,30 @@ fn operation_to_qasm( } } -fn get_extended_gate_definition(gate: StandardGate) -> Option<&'static str> { +// Definitions use only the original qelib1.inc, so no implicit extension +// dependencies (or additional dependency ordering) are needed. +fn get_extended_gate_definition(gate: &str) -> Option<&'static str> { match gate { - StandardGate::CRX => { + "crx" => { Some("gate crx(theta) a,b { h b;rz(theta/2) b; cx a,b; rz(-theta/2) b; cx a,b; h b;}") } - StandardGate::CRY => { - Some("gate cry(theta) a,b { ry(theta/2) b; cx a,b; ry(-theta/2) b; cx a,b; }") - } - StandardGate::RZZ => Some("gate rzz(theta) a,b { cx a,b; rz(theta) b; cx a,b; }"), - StandardGate::RXX => { - Some("gate rxx(theta) a,b { h a; h b; cx a,b; rz(theta) b; cx a,b; h a; h b; }") - } - StandardGate::RYY => Some( + "cry" => Some("gate cry(theta) a,b { ry(theta/2) b; cx a,b; ry(-theta/2) b; cx a,b; }"), + "rzz" => Some("gate rzz(theta) a,b { cx a,b; rz(theta) b; cx a,b; }"), + "rxx" => Some("gate rxx(theta) a,b { h a; h b; cx a,b; rz(theta) b; cx a,b; h a; h b; }"), + "ryy" => Some( "gate ryy(theta) a,b { rx(pi/2) a; rx(pi/2) b; cx a,b; rz(theta) b; cx a,b; rx(-pi/2) a; rx(-pi/2) b; }", ), - StandardGate::RZX => Some("gate rzx(theta) a,b { h b; cx a,b; rz(theta) b; cx a,b; h b; }"), + "rzx" => Some("gate rzx(theta) a,b { h b; cx a,b; rz(theta) b; cx a,b; h b; }"), + "swap" => Some("gate swap a,b { cx a,b; cx b,a; cx a,b; }"), + "cswap" => Some("gate cswap c,a,b { cx b,a; ccx c,a,b; cx b,a; }"), + // FSIM(theta,phi) = RXX(theta) RYY(theta) CP(-phi), inlined to + // keep the definition independent of other extension definitions. + "fsim" => Some(concat!( + "gate fsim(theta,phi) a,b { ", + "h a; h b; cx a,b; rz(theta) b; cx a,b; h a; h b; ", + "rx(pi/2) a; rx(pi/2) b; cx a,b; rz(theta) b; cx a,b; ", + "rx(-pi/2) a; rx(-pi/2) b; cu1(-phi) a,b; }" + )), _ => None, } } diff --git a/crates/cqlib-core/src/ir/qasm2/dump_test.rs b/crates/cqlib-core/src/ir/qasm2/dump_test.rs index 088f9e2..06520d7 100644 --- a/crates/cqlib-core/src/ir/qasm2/dump_test.rs +++ b/crates/cqlib-core/src/ir/qasm2/dump_test.rs @@ -112,6 +112,247 @@ fn assert_qasm_unitary_matches(qasm: &str, circuit: &Circuit, allow_global_phase assert_matrix_approx_eq(&actual, &expected, 1e-10); } +/// Force extension calls through their emitted definitions instead of the +/// loader's implicit Cqlib builtins. This also checks the definition bodies. +fn without_implicit_extension_gates(qasm: &str) -> String { + regex::Regex::new(r"\b(crx|cry|rxx|ryy|rzz|rzx|swap|cswap|fsim)\b") + .unwrap() + .replace_all(qasm, "exported_$1") + .into_owned() +} + +fn extension_test_circuit(gate: StandardGate, controlled: bool, values: [f64; 2]) -> Circuit { + let instruction = if controlled { + Instruction::McGate(Box::new(crate::circuit::gate::MCGate::new(1, gate))) + } else { + Instruction::Standard(gate) + }; + let mut circuit = Circuit::new(gate.num_qubits() + usize::from(controlled)); + let params: Vec = values[..gate.num_params()] + .iter() + .copied() + .map(Into::into) + .collect(); + circuit + .append(instruction, circuit.qubits(), params, None) + .unwrap(); + circuit +} + +fn assert_portable_gate_definition(gate: StandardGate, controlled: bool) { + for values in [ + [0.0, 0.0], + [0.731, -1.219], + [-std::f64::consts::PI, std::f64::consts::FRAC_PI_2], + ] { + let circuit = extension_test_circuit(gate, controlled, values); + let qasm = dumps(&circuit).unwrap(); + assert_qasm_unitary_matches(&without_implicit_extension_gates(&qasm), &circuit, false); + } +} + +#[test] +fn portable_qasm2_standard_swap_definition() { + assert_portable_gate_definition(StandardGate::SWAP, false); +} + +#[test] +fn portable_qasm2_standard_fsim_definition() { + assert_portable_gate_definition(StandardGate::FSIM, false); +} + +#[test] +fn portable_qasm2_controlled_rx_definition() { + assert_portable_gate_definition(StandardGate::RX, true); +} + +#[test] +fn portable_qasm2_controlled_ry_definition() { + assert_portable_gate_definition(StandardGate::RY, true); +} + +#[test] +fn portable_qasm2_controlled_swap_definition() { + assert_portable_gate_definition(StandardGate::SWAP, true); +} + +#[test] +fn portable_qasm2_standard_controlled_rotations_still_define_gates() { + assert_portable_gate_definition(StandardGate::CRX, false); + assert_portable_gate_definition(StandardGate::CRY, false); +} + +#[test] +fn portable_qasm2_sparse_qubits_preserve_numeric_basis_and_width() { + let qubits = [9, 2, 5].map(Qubit::new); + let mut circuit = Circuit::from_qubits(qubits.to_vec()).unwrap(); + circuit.x(qubits[0]).unwrap(); + circuit.cx(qubits[0], qubits[2]).unwrap(); + circuit.validate().unwrap(); + + let qasm = dumps(&circuit).unwrap(); + assert_qasm_unitary_matches(&qasm, &circuit, false); + let path = unique_temp_path("sparse_qubits"); + dump(&circuit, &path).unwrap(); + assert_eq!(fs::read_to_string(&path).unwrap(), qasm); + fs::remove_file(path).unwrap(); +} + +#[test] +fn portable_qasm2_dense_qubits_in_storage_order_keep_numeric_basis() { + let qubits = [2, 0, 1].map(Qubit::new); + let mut circuit = Circuit::from_qubits(qubits.to_vec()).unwrap(); + circuit.x(qubits[0]).unwrap(); + circuit.cx(qubits[0], qubits[2]).unwrap(); + assert_qasm_unitary_matches(&dumps(&circuit).unwrap(), &circuit, false); +} + +#[test] +fn portable_qasm2_sparse_qubits_map_measurement_reset_and_if() { + let qubits = [9, 2, 5].map(Qubit::new); + let mut circuit = Circuit::from_qubits(qubits.to_vec()).unwrap(); + circuit.barrier(qubits.to_vec()).unwrap(); + circuit.reset(qubits[0]).unwrap(); + let measured = circuit.measure(qubits[2]).unwrap(); + circuit + .if_( + ClassicalExpr::bit_to_bool(measured.expr()).unwrap(), + |body| { + body.x(qubits[1])?; + Ok(()) + }, + ) + .unwrap(); + let qasm = dumps(&circuit).unwrap(); + assert_qasm_contains_ordered( + &qasm, + &[ + "qreg q[3];", + "barrier q[2],q[0],q[1];", + "reset q[2];", + "measure q[1] -> v0[0];", + "if (v0 == 1) x q[0];", + ], + ); + verify_qasm_roundtrip(&qasm, 3); +} + +#[test] +fn portable_qasm2_definitions_in_custom_gate_bodies() { + for (gate, controlled) in [ + (StandardGate::RX, true), + (StandardGate::RY, true), + (StandardGate::SWAP, true), + (StandardGate::SWAP, false), + (StandardGate::FSIM, false), + ] { + let inner = extension_test_circuit(gate, controlled, [0.731, -1.219]); + let width = inner.num_qubits(); + let custom = inner.clone().to_gate("wrapped_custom").unwrap(); + let unitary = UnitaryGate::new("wrapped_unitary", width as u16, 0) + .with_circuit(Arc::new(FrozenCircuit::new(inner))) + .unwrap(); + for instruction in [custom, Instruction::UnitaryGate(Box::new(unitary))] { + let mut outer = Circuit::new(width); + outer + .append(instruction, outer.qubits(), std::iter::empty(), None) + .unwrap(); + let qasm = dumps(&outer).unwrap(); + assert_qasm_unitary_matches(&without_implicit_extension_gates(&qasm), &outer, false); + } + } +} + +#[test] +fn portable_qasm2_definitions_in_if_bodies() { + for (gate, controlled) in [ + (StandardGate::RX, true), + (StandardGate::RY, true), + (StandardGate::SWAP, true), + (StandardGate::SWAP, false), + (StandardGate::FSIM, false), + ] { + let inner = extension_test_circuit(gate, controlled, [0.731, -1.219]); + let operation = &inner.operations()[0]; + let mut outer = Circuit::new(inner.num_qubits()); + let measured = outer.measure(Qubit::new(0)).unwrap(); + outer + .if_( + ClassicalExpr::bit_to_bool(measured.expr()).unwrap(), + |body| { + body.append( + operation.instruction.clone(), + operation.qubits.iter().copied(), + [0.731, -1.219][..gate.num_params()] + .iter() + .copied() + .map(ParameterValue::from), + None, + )?; + Ok(()) + }, + ) + .unwrap(); + let qasm = without_implicit_extension_gates(&dumps(&outer).unwrap()); + verify_qasm_roundtrip(&qasm, outer.num_qubits()); + // The extension must be defined before the conditional invocation. + assert_qasm_contains_ordered(&qasm, &["gate exported_", "if (v0 == 1) exported_"]); + } +} + +#[test] +fn portable_qasm2_mixed_standard_and_mcgate_define_rotation_once() { + let mut circuit = extension_test_circuit(StandardGate::RX, true, [0.731, 0.0]); + circuit.crx(Qubit::new(0), Qubit::new(1), -0.2).unwrap(); + let qasm = dumps(&circuit).unwrap(); + assert_eq!(qasm.matches("gate crx(").count(), 1, "{qasm}"); + assert_qasm_unitary_matches(&without_implicit_extension_gates(&qasm), &circuit, false); +} + +#[test] +fn portable_qasm2_extension_definition_conflicts_are_rejected() { + for (name, gate, controlled) in [ + ("swap", StandardGate::SWAP, false), + ("fsim", StandardGate::FSIM, false), + ("cswap", StandardGate::SWAP, true), + ] { + let mut inner = Circuit::new(1); + inner.x(Qubit::new(0)).unwrap(); + let custom = inner.clone().to_gate(name).unwrap(); + let unitary = UnitaryGate::new(name, 1, 0) + .with_circuit(Arc::new(FrozenCircuit::new(inner))) + .unwrap(); + let opaque = UnitaryGate::new(name, 1, 0); + for instruction in [ + custom, + Instruction::UnitaryGate(Box::new(unitary)), + Instruction::UnitaryGate(Box::new(opaque)), + ] { + // A custom definition alone must remain valid even if its name + // happens to match an unused standard extension. + let mut custom_only = Circuit::new(1); + custom_only + .append( + instruction.clone(), + [Qubit::new(0)], + std::iter::empty(), + None, + ) + .unwrap(); + assert!(dumps(&custom_only).is_ok()); + + let mut circuit = extension_test_circuit(gate, controlled, [0.731, -1.219]); + circuit + .append(instruction, [Qubit::new(0)], std::iter::empty(), None) + .unwrap(); + assert!(matches!( + dumps(&circuit), + Err(QasmDumpError::FormatError(message)) if message.contains(name) + )); + } + } +} + /// Simple contains check (for backwards compatibility) fn assert_qasm_contains(qasm: &str, expected_lines: &[&str]) { for &line in expected_lines {