diff options
| author | hachem <im@hachem.wtf> | 2025-12-06 22:33:56 +0100 |
|---|---|---|
| committer | hachem <im@hachem.wtf> | 2025-12-06 22:33:56 +0100 |
| commit | 1fb7989e9405f9795a1d5b3953881b9bb8a137b4 (patch) | |
| tree | c680f041fe41e5ccd1e311ec7b1502abb96237af /tester/src/main.rs | |
| parent | edc6bae7bf5039c8cef2f2c9c164187478b1c7eb (diff) | |
[add]: custom gates + rework vertical renderer
Diffstat (limited to 'tester/src/main.rs')
| -rw-r--r-- | tester/src/main.rs | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/tester/src/main.rs b/tester/src/main.rs index ab4c78c..7981e8d 100644 --- a/tester/src/main.rs +++ b/tester/src/main.rs @@ -91,4 +91,71 @@ fn main() { complex.compute(); println!("{}", complex); + + print!("------\n\n"); + + println!("Custom Gate: Bell Pair Creator\n"); + + let bell_gate = CustomGateBuilder::new("BELL", 2).h(0).cnot(0, 1).build(); + + let mut custom_circuit = QuantumCircuit::new(4); + custom_circuit + .apply_custom(bell_gate.clone(), &[0, 1]) + .apply_custom(bell_gate.clone(), &[2, 3]); + + println!("Horizontal:"); + println!("{}", HorizontalRenderer::new(&custom_circuit)); + println!("Vertical:"); + println!("{}", VerticalRenderer::new(&custom_circuit)); + + custom_circuit.compute(); + println!("{}", custom_circuit); + + print!("------\n\n"); + + println!("Custom Gate: Swap via CNOTs\n"); + + let swap_gate = CustomGateBuilder::new("MYSWAP", 2) + .cnot(0, 1) + .cnot(1, 0) + .cnot(0, 1) + .build(); + + let mut swap_test = QuantumCircuit::new(2); + swap_test.x(0).apply_custom(swap_gate, &[0, 1]); + + println!("Horizontal:"); + println!("{}", HorizontalRenderer::new(&swap_test)); + println!("Vertical:"); + println!("{}", VerticalRenderer::new(&swap_test)); + + swap_test.compute(); + println!("{}", swap_test); + + print!("------\n\n"); + + println!("Custom Gate: Matrix-defined √X gate\n"); + + // √X gate (square root of NOT) + // When applied twice, it equals X gate + let sqrt_x_matrix = matrix!( + [complex!(0.5, 0.5), complex!(0.5, -0.5)]; + [complex!(0.5, -0.5), complex!(0.5, 0.5)] + ); + + let sqrt_x = CustomGate::from_matrix("√X", sqrt_x_matrix); + + let mut matrix_test = QuantumCircuit::new(1); + matrix_test + .apply_custom(sqrt_x.clone(), &[0]) + .apply_custom(sqrt_x, &[0]); // Two √X = X + + println!("Horizontal:"); + println!("{}", HorizontalRenderer::new(&matrix_test)); + println!("Vertical:"); + println!("{}", VerticalRenderer::new(&matrix_test)); + + matrix_test.compute(); + println!("{}", matrix_test); + println!("(Two √X gates should equal X, so |0⟩ becomes |1⟩)"); } |
