Skip to content

Commit

Permalink
Add missing operations
Browse files Browse the repository at this point in the history
  • Loading branch information
aborgna-q committed Apr 5, 2024
1 parent 93e813b commit 9f79823
Show file tree
Hide file tree
Showing 2 changed files with 194 additions and 2 deletions.
160 changes: 158 additions & 2 deletions src/opbox.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Data definition for box operations.
use std::collections::HashMap;

use crate::circuit_json::{
ClassicalExp, CompositeGate, Operation, Permutation, Register, SerialCircuit,
};
Expand Down Expand Up @@ -81,6 +83,26 @@ pub enum OpBox {
/// Config param for decomposition of Pauli exponentials.
cx_config: String,
},
/// An unordered collection of Pauli exponentials that can be synthesised in
/// any order, causing a change in the unitary operation. Synthesis order
/// depends on the synthesis strategy chosen only.
TermSequenceBox {
id: BoxID,
/// List of Symengine expressions.
pauli_gadgets: Vec<(Vec<String>, String)>,
/// Synthesis strategy. See [`PauliSynthStrat`].
#[serde(default)]
synthesis_strategy: PauliSynthStrat,
/// Partition strategy. See [`PauliPartitionStrat`].
#[serde(default)]
partitioning_strategy: PauliPartitionStrat,
/// Graph colouring method. See [`GraphColourMethod`].
#[serde(default)]
graph_colouring: GraphColourMethod,
/// Configurations for CXs upon decompose phase gadgets.
#[serde(default)]
cx_config_type: CXConfigType,
},
/// An operation capable of representing arbitrary Circuits made up of CNOT
/// and RZ, as a PhasePolynomial plus a boolean matrix representing an
/// additional linear transformation.
Expand Down Expand Up @@ -142,9 +164,15 @@ pub enum OpBox {
MultiplexedU2Box {
id: BoxID,
op_map: Vec<(Vec<bool>, Operation)>,
#[serde(default = "default_impl_diag")]
#[serde(default = "default_true")]
impl_diag: bool,
},
/// A user-defined multiplexed tensor product of U2 gates specified by a map
/// from bitstrings to lists of Op or a list of bitstring-list(Op s) pairs.
MultiplexedTensoredU2Box {
id: BoxID,
op_map: Vec<(Vec<bool>, Operation)>,
},
/// An operation that constructs a circuit to implement the specified
/// permutation of classical basis states.
ToffoliBox {
Expand All @@ -160,9 +188,54 @@ pub enum OpBox {
#[serde(default)]
rotation_axis: Option<OpType>,
},
/// An operation composed of ‘action’, ‘compute’ and ‘uncompute’ circuits
ConjugationBox {
id: BoxID,
/// Reversible computation.
compute: Box<Operation>,
/// Internal operation to be applied.
action: Box<Operation>,
/// Reverse uncomputation.
uncompute: Option<Box<Operation>>,
},
/// A placeholder operation that holds resource data. This box type cannot
/// be decomposed into a circuit. It only serves to record resource data for
/// a region of a circuit: for example, upper and lower bounds on gate
/// counts and depth. A circuit containing such a box cannot be executed.
DummyBox {
id: BoxID,
/// Number of qubits.
n_qubits: u32,
/// Number of bits.
n_bits: u32,
/// Dummy resource data.
resource_data: ResourceData,
},
/// A box for preparing quantum states using multiplexed-Ry and multiplexed-Rz gates.
StatePreparationBox {
id: BoxID,
/// Normalised statevector of complex numbers
statevector: Vec<(f32, f32)>,
/// Whether to implement the dagger of the state preparation circuit, default to false.
#[serde(default)]
is_inverse: bool,
/// Whether to explicitly set the state to zero initially (by default
/// the initial zero state is assumed and no explicit reset is applied).
#[serde(default)]
with_initial_reset: bool,
},
/// A box for synthesising a diagonal unitary matrix into a sequence of multiplexed-Rz gates.
DiagonalBox {
id: BoxID,
/// Diagonal entries.
diagonal: Vec<Vec<(f32, f32)>>,
/// Indicates whether the multiplexed-Rz gates take the shape of an upper triangle or a lower triangle. Default to true.
#[serde(default = "default_true")]
upper_triangle: bool,
},
}

fn default_impl_diag() -> bool {
fn default_true() -> bool {
true
}

Expand All @@ -176,10 +249,93 @@ pub enum ToffoliBoxSynthStrat {
Cycle,
}

/// Strategies for synthesising PauliBoxes.
#[derive(Deserialize, Serialize, Clone, Debug, PartialEq, Default)]
#[non_exhaustive]
pub enum PauliSynthStrat {
/// Synthesise gadgets individually.
Individual,
/// Synthesise gadgets using an efficient pairwise strategy from Cowtan et
/// al (https://arxiv.org/abs/1906.01734).
Pairwise,
/// Synthesise gadgets in commuting sets.
#[default]
Sets,
}

/// Strategies for partitioning Pauli tensors.
#[derive(Deserialize, Serialize, Clone, Debug, PartialEq, Default)]
#[non_exhaustive]
pub enum PauliPartitionStrat {
/// Build sets of Pauli tensors in which each qubit has the same Pauli or
/// Pauli.I. Requires no additional CX gates for diagonalisation.
NonConflictingSets,
/// Build sets of mutually commuting Pauli tensors. Requires O(n^2) CX gates to diagonalise.
#[default]
CommutingSets,
}

/// Available methods to perform graph colouring.
#[derive(Deserialize, Serialize, Clone, Debug, PartialEq, Default)]
#[non_exhaustive]
pub enum GraphColourMethod {
/// Does not build the graph before performing the colouring; partitions
/// while iterating through the Pauli tensors in the input order.
#[default]
Lazy,
/// Builds the graph and then greedily colours by iterating through the
/// vertices, with the highest degree first.
LargestFirst,
/// Builds the graph and then systematically checks all possibilities until
/// it finds a colouring with the minimum possible number of colours. Such
/// colourings need not be unique. Exponential time in the worst case, but
/// often runs much faster.
Exhaustive,
}

/// Available configurations for CXs upon decompose phase gadgets
#[derive(Deserialize, Serialize, Clone, Debug, PartialEq, Default)]
#[non_exhaustive]
pub enum CXConfigType {
/// Linear nearest neighbour CX sequence. Linear depth.
Snake,
/// Every CX has same target, linear depth, good for gate cancellation.
Star,
/// Balanced tree: logarithmic depth, harder to route.
#[default]
Tree,
/// Support for multi-qubit architectures, decomposing to 3-qubit XXPhase3
/// gates instead of CXs where possible.
MultiQGate,
}

/// A simple struct for Pauli strings with +/- phase, used to represent Pauli
/// strings in a stabiliser subgroup.
#[derive(Deserialize, Serialize, Clone, Debug, PartialEq, Eq, Hash)]
pub struct PauliStabiliser {
coeff: bool,
string: Vec<String>,
}

/// An object holding resource data for use in a [`OpType::DummyBox`].
#[derive(Deserialize, Serialize, Clone, Debug, PartialEq)]
pub struct ResourceData {
/// Dictionary of counts of selected [`OpType`]s.
pub op_type_count: HashMap<OpType, ResourceBounds>,
/// Overall gate depth.
pub gate_depth: ResourceBounds,
/// Dictionary of depths of selected [`OpType`]s.
pub op_type_depth: HashMap<OpType, ResourceBounds>,
/// Overall two-qubit-gate depth.
pub two_qubit_gate_depth: ResourceBounds,
}

/// Structure holding a minimum and maximum value of some resource, where both
/// values are unsigned integers.
#[derive(Deserialize, Serialize, Clone, Debug, PartialEq)]
pub struct ResourceBounds {
/// Minimum value.
pub min: u32,
/// Maximum value.
pub max: u32,
}
36 changes: 36 additions & 0 deletions src/optype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ pub enum OpType {
/// A general classical operation where all inputs are also outputs
ClassicalTransform,

/// Op containing a classical wasm function call.
WASM,

/// An operation to set some bits to specified values
SetBits,

Expand All @@ -68,6 +71,9 @@ pub enum OpType {
/// A classical operation applied to multiple bits simultaneously
MultiBit,

/// Global phase $\alpha \mapsto \[\e^{i\pi\alpha}\]$
Phase,

/// \f$ \left[ \begin{array}{cc} 1 & 0 \\ 0 & -1 \end{array} \right] \f$
Z,

Expand Down Expand Up @@ -207,6 +213,12 @@ pub enum OpType {
/// \end{array} \right] \f$
CSXdg,

/// Controlled \ref OpType::S gate
CS,

/// Controlled \ref OpType::Sdg gate
CSdg,

/// Controlled \ref OpType::Rz
///
/// \f$ \mathrm{CRz}(\alpha) = \left[ \begin{array}{cccc} 1 & 0 & 0 & 0 \\ 0
Expand Down Expand Up @@ -359,6 +371,12 @@ pub enum OpType {
/// Multiply-controlled \ref OpType::X
CnX,

/// Multiply-controlled \ref OpType::Y
CnY,

/// Multiply-controlled \ref OpType::Z
CnZ,

/// See \ref CircBox
CircBox,

Expand All @@ -383,6 +401,9 @@ pub enum OpType {
/// See \ref PauliExpCommutingSetBox
PauliExpCommutingSetBox,

/// See \ref TermSequenceBox
TermSequenceBox,

/// NYI
CliffBox,

Expand Down Expand Up @@ -419,8 +440,23 @@ pub enum OpType {
/// See \ref MultiplexedU2Box
MultiplexedU2Box,

/// See \ref MultiplexedTensoredU2Box
MultiplexedTensoredU2Box,

/// See \ref ToffoliBox
ToffoliBox,

/// See \ref ConjugationBox
ConjugationBox,

/// See \ref DummyBox
DummyBox,

/// See \ref StatePreparationBox
StatePreparationBox,

/// See \ref DiagonalBox
DiagonalBox,
}

#[cfg(feature = "pyo3")]
Expand Down

0 comments on commit 9f79823

Please sign in to comment.