Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: map_params helpers on the parametric structs #65

Merged
merged 1 commit into from
Aug 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
}
}
}
Loading