Skip to content

Commit

Permalink
feat: map_params helpers on the parametric structs (#65)
Browse files Browse the repository at this point in the history
Some helpers to convert between parameter types without too much
boilerplate.

I wanted to add `ToOwned` instances or something similar, but most trait
implementations clash with blanket impls, so these are just struct
methods.
  • Loading branch information
aborgna-q authored Aug 7, 2024
1 parent 213c93d commit 6d8aca4
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions src/circuit_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,4 +252,62 @@ impl<P> Operation<P> {
..Operation::default()
}
}

/// Applies a function over the parameters of the operation.
///
/// Returns a new Operation with the same data, but with a new generic
/// type for the parameters.
pub fn map_params<Q>(self, f: impl FnMut(P) -> Q) -> Operation<Q> {
Operation {
op_type: self.op_type,
n_qb: self.n_qb,
data: self.data,
params: self
.params
.map(|params| params.into_iter().map(f).collect()),
op_box: self.op_box,
signature: self.signature,
conditional: self.conditional,
classical: self.classical,
wasm: self.wasm,
}
}
}

impl<P> Command<P> {
/// Applies a function over the parameters of the command.
///
/// Returns a new Command with the same data, but with a new generic type
/// for the parameters.
pub fn map_params<Q>(self, f: impl FnMut(P) -> Q) -> Command<Q> {
Command {
op: self.op.map_params(f),
args: self.args,
opgroup: self.opgroup,
}
}
}

impl<P> SerialCircuit<P> {
/// Applies a function over the parameters of the circuit.
///
/// Returns a new SerialCircuit with the same data, but with a new generic
/// type for the parameters.
pub fn map_params<Q>(self, mut f: impl FnMut(P) -> Q) -> SerialCircuit<Q> {
let phase = f(self.phase);
let commands = self
.commands
.into_iter()
.map(|c| c.map_params(&mut f))
.collect();
SerialCircuit {
name: self.name,
phase,
commands,
qubits: self.qubits,
bits: self.bits,
implicit_permutation: self.implicit_permutation,
number_of_ws: self.number_of_ws,
}
}
}

0 comments on commit 6d8aca4

Please sign in to comment.