qft/
qft.rs

1// SPDX-FileCopyrightText: 2024 Evandro Chagas Ribeiro da Rosa <evandro@quantuloop.com>
2//
3// SPDX-License-Identifier: Apache-2.0
4
5use std::f64::consts::PI;
6
7use ket::{execution::LogicalQubit, prelude::*};
8
9fn qft(process: &mut Process, qubits: &[LogicalQubit], do_swap: bool) -> Result<(), KetError> {
10    if qubits.len() == 1 {
11        return process.gate(QuantumGate::Hadamard, qubits[0]);
12    }
13
14    let init = &qubits[..qubits.len() - 1];
15    let last = qubits[qubits.len() - 1];
16    process.gate(QuantumGate::Hadamard, last)?;
17    for (i, c) in init.iter().enumerate() {
18        c1gate(
19            process,
20            QuantumGate::Phase(PI / 2.0_f64.powi(i as i32 + 1)),
21            *c,
22            last,
23        )?;
24    }
25    qft(process, init, false)?;
26
27    if do_swap {
28        for i in 0..qubits.len() / 2 {
29            swap(process, qubits[i], qubits[qubits.len() - i - 1])?;
30        }
31    }
32
33    Ok(())
34}
35
36fn main() -> Result<(), KetError> {
37    let config = Configuration {
38        num_qubits: 12,
39        qpu: Some(QPU::new(
40            // 0--1--2--3
41            // |  |  |  |
42            // 4--5--6--7
43            // |  |  |  |
44            // 8--9--A--B
45            Some(ket::ex_arch::GRID12.to_vec()),
46            12,
47            U2Gates::RzSx,
48            U4Gate::CX,
49        )),
50        ..Default::default()
51    };
52
53    let mut process = Process::new(config);
54
55    let size = 12;
56    let qubits: Vec<_> = (0..size).map(|_| process.alloc().unwrap()).collect();
57
58    qft(&mut process, &qubits, true)?;
59
60    process.transpile();
61
62    println!("{:#?}", process.metadata());
63
64    Ok(())
65}