Skip to content

Commit 2e33bbd

Browse files
committed
implement rewriting logic on ResourceScope
1 parent e8f0325 commit 2e33bbd

11 files changed

+782
-49
lines changed

tket/examples/circuit-matcher.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ impl CircuitMatcher for CliffordMatcher {
103103
fn main() {
104104
const CIRCUIT: &str = r#"{"bits": [], "commands": [{"args": [["q", [0]], ["q", [1]]], "op": {"type": "CX"}}, {"args": [["q", [0]]], "op": {"params": ["0.5"], "type": "Rz"}}, {"args": [["q", [1]]], "op": {"type": "V"}}, {"args": [["q", [0]], ["q", [2]]], "op": {"type": "CX"}}, {"args": [["q", [0]], ["q", [1]]], "op": {"type": "CX"}}, {"args": [["q", [2]]], "op": {"type": "S"}}, {"args": [["q", [0]]], "op": {"params": ["0.111"], "type": "Rz"}}, {"args": [["q", [2]]], "op": {"type": "T"}}, {"args": [["q", [1]], ["q", [2]]], "op": {"type": "CX"}}, {"args": [["q", [1]]], "op": {"type": "T"}}, {"args": [["q", [2]]], "op": {"type": "S"}}, {"args": [["q", [0]], ["q", [1]]], "op": {"type": "CX"}}], "created_qubits": [], "discarded_qubits": [], "implicit_permutation": [[["q", [0]], ["q", [0]]], [["q", [1]], ["q", [1]]], [["q", [2]], ["q", [2]]]], "phase": "0.0", "qubits": [["q", [0]], ["q", [1]], ["q", [2]]]}"#;
105105
let ser_circ: SerialCircuit = serde_json::from_str(CIRCUIT).unwrap();
106-
let circuit = ResourceScope::from_circuit(ser_circ.decode().unwrap());
106+
let circuit = ResourceScope::from_circuit(ser_circ.decode(Default::default()).unwrap());
107107

108108
let matcher = CliffordMatcher {
109109
allowed_num_cx: 2..4,

tket/src/resource.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ use hugr::{
5454
};
5555
pub use interval::{Interval, InvalidInterval};
5656
use itertools::Itertools;
57-
pub use scope::{ResourceScope, ResourceScopeConfig};
57+
pub use scope::{CircuitRewriteError, ResourceScope, ResourceScopeConfig};
5858
pub use types::{CircuitUnit, Position, ResourceAllocator, ResourceId};
5959

6060
use crate::{
@@ -82,7 +82,8 @@ impl<H: HugrMut> ResourceScope<H> {
8282

8383
/// Register a rewrite applied to the circuit.
8484
///
85-
/// Returns `true` if the rewrite was successfully registered, or `false` if it was ignored.
85+
/// Returns `true` if the rewrite was successfully registered, or `false` if
86+
/// it was ignored.
8687
#[inline]
8788
pub fn add_rewrite_trace(&mut self, rewrite: impl Into<RewriteTrace>) -> bool {
8889
self.as_circuit_mut().add_rewrite_trace(rewrite)

tket/src/resource/scope.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
//! tracking within a specific region of a HUGR, computing resource paths and
55
//! providing efficient lookup of circuit units associated with ports.
66
7-
use std::collections::BTreeSet;
8-
use std::{cmp, iter};
7+
mod patch;
8+
pub use patch::CircuitRewriteError;
9+
10+
use std::{cmp, collections::BTreeSet, iter};
911

1012
use crate::resource::flow::{DefaultResourceFlow, ResourceFlow};
1113
use crate::resource::types::{CircuitUnit, PortMap};
@@ -185,17 +187,15 @@ impl<H: HugrView> ResourceScope<H> {
185187
self.hugr
186188
}
187189

188-
pub(crate) fn hugr_mut(&mut self) -> &mut H {
189-
&mut self.hugr
190-
}
191-
192190
/// Wrap the underlying HUGR in a Circuit as reference.
193191
pub fn as_circuit(&self) -> Circuit<&H> {
194192
Circuit::new(self.hugr())
195193
}
196194

197-
pub(crate) fn as_circuit_mut(&mut self) -> Circuit<&mut H> {
198-
Circuit::new(self.hugr_mut())
195+
/// Careful: this will not update the circuit units, so do not modify
196+
/// the HUGR using this.
197+
pub(super) fn as_circuit_mut(&mut self) -> Circuit<&mut H> {
198+
Circuit::new(&mut self.hugr)
199199
}
200200

201201
/// Get the underlying subgraph, or `None` if the circuit is empty.

0 commit comments

Comments
 (0)