Skip to content

Commit

Permalink
added functionality for porting
Browse files Browse the repository at this point in the history
  • Loading branch information
Procatv committed Jul 9, 2024
1 parent e5533fd commit 597d9bd
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 25 deletions.
29 changes: 28 additions & 1 deletion crates/accelerate/src/synthesis/permutation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,38 @@ fn _synth_permutation_acg(py: Python, pattern: PyArrayLike1<i64>) -> PyResult<Ci
)
}

/// Synthesize a permutation circuit for a linear nearest-neighbor
/// architecture using the Kutin, Moulton, Smithline method.
#[pyfunction]
#[pyo3(signature = (pattern))]
pub fn _synth_permutation_depth_lnn_kms(py: Python, pattern: PyArrayLike1<i64>) -> PyResult<CircuitData> {
let inverted = utils::invert(&pattern.as_array());
let view = inverted.view();
let num_qubits = view.len();

let swap_layer: Vec<(usize, usize)> = utils::_create_swap_layer(&view);

println!("Swap layer: {:?}" , swap_layer);
CircuitData::from_standard_gates(
py,
num_qubits as u32,
swap_layer.iter().map(|(i, j)| {
(
StandardGate::SwapGate,
smallvec![],
smallvec![Qubit(*i as u32), Qubit(*j as u32)],
)
}),
Param::Float(0.0),
)
}

#[pymodule]
pub fn permutation(m: &Bound<PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(_validate_permutation, m)?)?;
m.add_function(wrap_pyfunction!(_inverse_pattern, m)?)?;
m.add_function(wrap_pyfunction!(_synth_permutation_basic, m)?)?;
m.add_function(wrap_pyfunction!(_synth_permutation_acg, m)?)?;
m.add_function(wrap_pyfunction!(_synth_permutation_depth_lnn_kms, m)?)?;
Ok(())
}
}
17 changes: 17 additions & 0 deletions crates/accelerate/src/synthesis/permutation/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,20 @@ pub fn decompose_cycles(cycles: &Vec<Vec<usize>>) -> Vec<(usize, usize)> {

swaps
}

// create swaps
//Need to fix the below implementation
pub fn _create_swap_layer(pattern: &ArrayView1<usize>) -> Vec<(usize, usize)> {
// let mut permutation: Vec<usize> = pattern.iter().map(|&x| x as usize).collect();
let mut pattern_vec = pattern.to_vec();
let num_qubits = pattern_vec.len();
let mut gates = Vec::new();

for j in (0..num_qubits - 1).step_by(2) {
if pattern_vec[j] > pattern_vec[j + 1] {
gates.push((j, j +1));
pattern_vec.swap(j, j + 1);
}
}
gates
}
26 changes: 2 additions & 24 deletions qiskit/synthesis/permutation/permutation_lnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from __future__ import annotations
import numpy as np
from qiskit.circuit.quantumcircuit import QuantumCircuit
from .permutation_utils import _inverse_pattern
from qiskit._accelerate.synthesis.permutation import _synth_permutation_depth_lnn_kms


def synth_permutation_depth_lnn_kms(pattern: list[int] | np.ndarray[int]) -> QuantumCircuit:
Expand Down Expand Up @@ -49,26 +49,4 @@ def synth_permutation_depth_lnn_kms(pattern: list[int] | np.ndarray[int]) -> Qua
# In the permutation synthesis code below the notation is opposite:
# [2, 4, 3, 0, 1] means that 0 maps to 2, 1 to 3, 2 to 3, 3 to 0, and 4 to 1.
# This is why we invert the pattern.
cur_pattern = _inverse_pattern(pattern)

num_qubits = len(cur_pattern)
qc = QuantumCircuit(num_qubits)

# add conditional odd-even swap layers
for i in range(num_qubits):
_create_swap_layer(qc, cur_pattern, i % 2)

return qc


def _create_swap_layer(qc, pattern, starting_point):
"""Implements a single swap layer, consisting of conditional swaps between each
neighboring couple. The starting_point is the first qubit to use (either 0 or 1
for even or odd layers respectively). Mutates both the quantum circuit ``qc``
and the permutation pattern ``pattern``.
"""
num_qubits = len(pattern)
for j in range(starting_point, num_qubits - 1, 2):
if pattern[j] > pattern[j + 1]:
qc.swap(j, j + 1)
pattern[j], pattern[j + 1] = pattern[j + 1], pattern[j]
return QuantumCircuit._from_circuit_data(_synth_permutation_depth_lnn_kms(pattern))
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
upgrade_synthesis:
- |
Port :func:`.synth_permutation_depth_lnn_kms`, used to synthesize qubit permutations, to Rust.
1 change: 1 addition & 0 deletions test/python/synthesis/test_permutation_synthesis.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
synth_permutation_depth_lnn_kms,
synth_permutation_basic,
synth_permutation_reverse_lnn_kms,
synth_permutation_depth_lnn_kms,
)
from qiskit.synthesis.permutation.permutation_utils import (
_inverse_pattern,
Expand Down

0 comments on commit 597d9bd

Please sign in to comment.