Skip to content

Commit 7a1d57a

Browse files
committed
implementation
1 parent bb0dd6a commit 7a1d57a

File tree

3 files changed

+37
-12
lines changed

3 files changed

+37
-12
lines changed

crates/lib/src/compiler/isa/edge.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ impl Edge {
5858

5959
Ok(())
6060
}
61+
62+
pub(crate) fn has_valid_operations(&self) -> bool {
63+
!self.dead
64+
}
6165
}
6266

6367
/// All the error which can occur from within this module.

crates/lib/src/compiler/isa/mod.rs

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,13 @@ impl TryFrom<InstructionSetArchitecture> for Compiler {
6464
let qubits = qubits
6565
.into_iter()
6666
.map(|(k, v)| (k.to_string(), v))
67+
.filter(|(_, q)| q.has_valid_operations())
68+
.collect();
69+
let edges = edges
70+
.into_iter()
71+
.map(|(k, v)| (k.to_string(), v))
72+
.filter(|(_, e)| e.has_valid_operations())
6773
.collect();
68-
let edges = edges.into_iter().map(|(k, v)| (k.to_string(), v)).collect();
6974
Ok(Self { qubits, edges })
7075
}
7176
}
@@ -186,22 +191,33 @@ mod describe_compiler_isa {
186191
fn compiler_excludes_qubits_with_no_operations() {
187192
let input = read_to_string("tests/qcs-isa-with-dead-qubits.json")
188193
.expect("Could not read ISA with dead qubits");
189-
let expected_json = read_to_string("tests/compiler-isa-with-dead-qubits.json")
190-
.expect("Could not read expected output with dead qubits");
191194
let qcs_isa: InstructionSetArchitecture =
192195
serde_json::from_str(&input).expect("Could not deserialize ISA with dead qubits");
193-
let expected: Value = serde_json::from_str(&expected_json)
194-
.expect("Could not deserialize expected output with dead qubits");
195-
196-
let compiler_isa =
197-
Compiler::try_from(qcs_isa).expect("Could not convert ISA with dead qubits to CompilerIsa");
198-
199-
assert!(!compiler_isa.qubits.contains_key("31"), "Qubit 31 should not be in the CompilerIsa");
196+
let compiler_isa = Compiler::try_from(qcs_isa)
197+
.expect("Could not convert ISA with dead qubits to CompilerIsa");
198+
199+
assert!(
200+
!compiler_isa.qubits.contains_key("31"),
201+
"Qubit 31 should not be in the CompilerIsa"
202+
);
203+
assert!(
204+
!compiler_isa.edges.contains_key("31-32"),
205+
"edge with Qubit 31 should not be in the CompilerIsa"
206+
);
207+
208+
let input_without_dead = read_to_string("tests/qcs-isa-excluding-dead-qubits.json")
209+
.expect("Could not read ISA that excludes dead qubits");
210+
let isa_without_dead: InstructionSetArchitecture = serde_json::from_str(&input_without_dead)
211+
.expect("Could not read ISA that excludes dead qubits");
212+
let compiler_isa_excluding_dead = Compiler::try_from(isa_without_dead)
213+
.expect("Could not convert ISA with manually excluded dead qubits to CompilerIsa");
200214

201215
let serialized = serde_json::to_value(compiler_isa)
202-
.expect("Unable to serialize CompilerIsa with dead qubits");
216+
.expect("Unable to serialize CompilerIsa");
217+
let serialized_without_dead = serde_json::to_value(compiler_isa_excluding_dead)
218+
.expect("Unable to serialize CompilerIsa");
203219

204-
let result = json_is_equivalent(&serialized, &expected);
220+
let result = json_is_equivalent(&serialized, &serialized_without_dead);
205221
result.expect("JSON was not equivalent");
206222
}
207223
}

crates/lib/src/compiler/isa/qubit.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ impl Qubit {
7070
self.dead = false;
7171
Ok(())
7272
}
73+
74+
/// Check if the qubit has any valid operations (gates).
75+
pub(crate) fn has_valid_operations(&self) -> bool {
76+
!self.gates.is_empty()
77+
}
7378
}
7479

7580
/// All the errors that can occur within this module.

0 commit comments

Comments
 (0)