def test_empty(self) -> None: circuit = Circuit(1) assert circuit.get_num_operations() == 0 circuit = Circuit(4) assert circuit.get_num_operations() == 0 circuit = Circuit(4, [2, 3, 4, 5]) assert circuit.get_num_operations() == 0
def test_adding_gate(self) -> None: circuit = Circuit(1) assert circuit.get_num_operations() == 0 circuit.append_gate(U3Gate(), [0]) assert circuit.get_num_operations() == 1 circuit.append_gate(U3Gate(), [0]) assert circuit.get_num_operations() == 2 circuit.append_gate(U3Gate(), [0]) assert circuit.get_num_operations() == 3
def test_inserting_gate(self) -> None: circuit = Circuit(1) assert circuit.get_num_operations() == 0 circuit.insert_gate(0, U3Gate(), [0]) assert circuit.get_num_operations() == 1 circuit.insert_gate(0, U3Gate(), [0]) assert circuit.get_num_operations() == 2 circuit.insert_gate(0, U3Gate(), [0]) assert circuit.get_num_operations() == 3
def test_removing_gate(self) -> None: circuit = Circuit(1) circuit.append_gate(U3Gate(), [0]) circuit.append_gate(U3Gate(), [0]) circuit.append_gate(U3Gate(), [0]) assert circuit.get_num_operations() == 3 circuit.remove(U3Gate()) assert circuit.get_num_operations() == 2 circuit.remove(U3Gate()) assert circuit.get_num_operations() == 1 circuit.remove(U3Gate()) assert circuit.get_num_operations() == 0
def test_correctness_3(self) -> None: circuit = Circuit(5) wide_gate = IdentityGate(3) circuit.append_gate(HGate(), [1]) circuit.append_gate(CNOTGate(), [2, 3]) circuit.append_gate(wide_gate, [1, 2, 3]) circuit.append_gate(CNOTGate(), [1, 2]) circuit.append_gate(HGate(), [3]) circuit.append_gate(XGate(), [0]) circuit.append_gate(XGate(), [0]) circuit.append_gate(XGate(), [0]) circuit.append_gate(XGate(), [4]) circuit.append_gate(XGate(), [4]) circuit.append_gate(XGate(), [4]) utry = circuit.get_unitary() circuit.fold(circuit.get_region([(0, 2), (1, 1), (2, 1)])) assert circuit.get_num_operations() == 9 assert circuit.get_depth() == 3 assert circuit.count(HGate()) == 2 assert circuit.count(XGate()) == 6 assert isinstance(circuit[1, 1].gate, CircuitGate) test_gate: CircuitGate = circuit[1, 1].gate assert test_gate._circuit[0, 1].gate is CNOTGate() assert test_gate._circuit[0, 2].gate is CNOTGate() assert test_gate._circuit[1, 0].gate is wide_gate assert test_gate._circuit[1, 1].gate is wide_gate assert test_gate._circuit[1, 2].gate is wide_gate check_no_idle_cycles(circuit) assert np.allclose(utry.get_numpy(), circuit.get_unitary().get_numpy())
def test_append_gate_4(self, qudit_index: int) -> None: circuit = Circuit(4) circuit.append_gate(CNOTGate(), [0, 1]) circuit.append_gate(CNOTGate(), [1, 2]) circuit.append_gate(CNOTGate(), [2, 3]) circuit.append_gate(CNOTGate(), [0, 1]) circuit.append_gate(CNOTGate(), [1, 2]) circuit.append_gate(CNOTGate(), [2, 3]) circuit.append_gate(CNOTGate(), [0, 1]) circuit.append_gate(CNOTGate(), [1, 2]) circuit.append_gate(CNOTGate(), [2, 3]) circuit.pop_qudit(qudit_index) assert circuit.get_size() == 3 assert len(circuit.get_radixes()) == 3 assert circuit.get_radixes().count(2) == 3 assert circuit.get_num_operations() == 6 assert circuit[0, 0].gate == CNOTGate() assert circuit[0, 0].location == (0, 1) assert circuit[1, 1].gate == CNOTGate() assert circuit[1, 1].location == (1, 2) assert circuit[2, 0].gate == CNOTGate() assert circuit[2, 0].location == (0, 1) assert circuit[3, 1].gate == CNOTGate() assert circuit[3, 1].location == (1, 2) assert circuit[4, 0].gate == CNOTGate() assert circuit[4, 0].location == (0, 1) assert circuit[5, 1].gate == CNOTGate() assert circuit[5, 1].location == (1, 2)
def test_multi_qudit(self, cycle_index: int) -> None: circuit = Circuit(4, [2, 2, 3, 3]) circuit.append_gate( ConstantUnitaryGate(np.identity(36), [2, 2, 3, 3]), [0, 1, 2, 3], ) circuit.append_gate( ConstantUnitaryGate(np.identity(36), [2, 2, 3, 3]), [0, 1, 2, 3], ) circuit.append_gate( ConstantUnitaryGate(np.identity(36), [2, 2, 3, 3]), [0, 1, 2, 3], ) assert circuit.get_num_cycles() == 3 assert circuit.get_num_operations() == 3 circuit.pop_cycle(cycle_index) assert circuit.get_num_cycles() == 2 assert circuit.get_num_operations() == 2
def run(self, circuit: Circuit, data: dict[str, Any]) -> None: """Perform the pass's operation, see BasePass for more info.""" stats: dict[str, Any] = {} stats['cycles'] = circuit.get_num_cycles() stats['num_ops'] = circuit.get_num_operations() stats['cgraph'] = circuit.get_coupling_graph() stats['depth'] = circuit.get_depth() stats['gate_counts'] = { gate: circuit.count(gate) for gate in circuit.get_gate_set() } if self.key not in data: data[self.key] = [] data[self.key].append(stats)
def test_r1(self, r3_qubit_circuit: Circuit) -> None: assert r3_qubit_circuit.get_num_operations() == 10 r3_qubit_circuit.append_gate(U3Gate(), [0]) assert r3_qubit_circuit.get_num_operations() == 11 r3_qubit_circuit.insert_gate(0, U3Gate(), [1]) assert r3_qubit_circuit.get_num_operations() == 12 r3_qubit_circuit.insert_gate(0, CNOTGate(), [0, 2]) assert r3_qubit_circuit.get_num_operations() == 13 r3_qubit_circuit.remove(U3Gate()) assert r3_qubit_circuit.get_num_operations() == 12 r3_qubit_circuit.remove(CNOTGate()) assert r3_qubit_circuit.get_num_operations() == 11
def test_multi_gate_1( self, qudit_index: int, gen_random_utry_np: Any, ) -> None: circuit = Circuit(4) three_qubit_gate = ConstantUnitaryGate(gen_random_utry_np(8)) circuit.append_gate(three_qubit_gate, [1, 2, 3]) circuit.append_gate(three_qubit_gate, [0, 2, 3]) circuit.append_gate(three_qubit_gate, [0, 1, 3]) circuit.append_gate(three_qubit_gate, [0, 1, 2]) circuit.pop_qudit(qudit_index) assert circuit.get_size() == 3 assert len(circuit.get_radixes()) == 3 assert circuit.get_radixes().count(2) == 3 assert circuit.get_num_operations() == 1 assert circuit.get_num_cycles() == 1 assert circuit[0, 0].gate == three_qubit_gate assert circuit[0, 0].location == (0, 1, 2) assert circuit[0, 1].gate == three_qubit_gate assert circuit[0, 1].location == (0, 1, 2) assert circuit[0, 2].gate == three_qubit_gate assert circuit[0, 2].location == (0, 1, 2)
def test_type(self, r6_qudit_circuit: Circuit) -> None: assert isinstance(r6_qudit_circuit.get_num_operations(), int)
def test_get_num_operations(self, simple_circuit: Circuit) -> None: assert simple_circuit.get_num_operations() == 4
def test_r6_depth(self, r6_qudit_circuit: Circuit) -> None: assert r6_qudit_circuit.get_num_operations() == 10
def test_correctness_1(self) -> None: circuit = Circuit(4) wide_gate = IdentityGate(4) circuit.append_gate(wide_gate, [0, 1, 2, 3]) circuit.append_gate(wide_gate, [0, 1, 2, 3]) circuit.append_gate(wide_gate, [0, 1, 2, 3]) circuit.append_gate(wide_gate, [0, 1, 2, 3]) assert circuit.get_num_operations() == 4 assert circuit.get_depth() == 4 utry = circuit.get_unitary() circuit.fold(circuit.get_region([(0, 0), (1, 0)])) assert circuit.get_num_operations() == 3 assert circuit.get_depth() == 3 check_no_idle_cycles(circuit) for q in range(4): assert isinstance(circuit[0, q].gate, CircuitGate) for c in range(1, 3, 1): for q in range(4): assert isinstance(circuit[c, q].gate, IdentityGate) assert isinstance(circuit[c, q].gate, IdentityGate) test_gate: CircuitGate = circuit[0, 0].gate # type: ignore assert test_gate._circuit.get_num_operations() == 2 assert test_gate._circuit.get_num_cycles() == 2 for q in range(4): assert isinstance(test_gate._circuit[0, q].gate, IdentityGate) assert isinstance(test_gate._circuit[1, q].gate, IdentityGate) circuit.fold(circuit.get_region([(1, 0), (2, 0)])) assert circuit.get_num_operations() == 2 assert circuit.get_depth() == 2 check_no_idle_cycles(circuit) for c in range(2): for q in range(4): assert isinstance(circuit[c, q].gate, CircuitGate) test_gate: CircuitGate = circuit[0, 0].gate # type: ignore assert test_gate._circuit.get_num_operations() == 2 assert test_gate._circuit.get_num_cycles() == 2 for q in range(4): assert isinstance(test_gate._circuit[0, q].gate, IdentityGate) assert isinstance(test_gate._circuit[1, q].gate, IdentityGate) test_gate: CircuitGate = circuit[1, 0].gate # type: ignore assert test_gate._circuit.get_num_operations() == 2 assert test_gate._circuit.get_num_cycles() == 2 for q in range(4): assert isinstance(test_gate._circuit[0, q].gate, IdentityGate) assert isinstance(test_gate._circuit[1, q].gate, IdentityGate) circuit.fold(circuit.get_region([(0, 0), (1, 0)])) assert circuit.get_num_operations() == 1 assert circuit.get_depth() == 1 check_no_idle_cycles(circuit) for q in range(4): assert isinstance(circuit[0, q].gate, CircuitGate) test_gate: CircuitGate = circuit[0, 0].gate # type: ignore assert test_gate._circuit.get_num_operations() == 2 assert test_gate._circuit.get_num_cycles() == 2 for q in range(4): assert isinstance(test_gate._circuit[0, q].gate, CircuitGate) assert isinstance(test_gate._circuit[1, q].gate, CircuitGate) inner_gate1: CircuitGate = test_gate._circuit[0, 0].gate # type: ignore inner_gate2: CircuitGate = test_gate._circuit[1, 0].gate # type: ignore assert inner_gate1._circuit.get_num_operations() == 2 assert inner_gate1._circuit.get_num_cycles() == 2 for q in range(4): assert isinstance(inner_gate1._circuit[0, q].gate, IdentityGate) assert isinstance(inner_gate1._circuit[1, q].gate, IdentityGate) assert isinstance(inner_gate2._circuit[0, q].gate, IdentityGate) assert isinstance(inner_gate2._circuit[1, q].gate, IdentityGate) check_no_idle_cycles(circuit) assert np.allclose(utry.get_numpy(), circuit.get_unitary().get_numpy())
def test_get_num_operations(self, swap_circuit: Circuit) -> None: assert swap_circuit.get_num_operations() == 3
def test_get_num_operations(self, toffoli_circuit: Circuit) -> None: assert toffoli_circuit.get_num_operations() == 15
def test_r3_qutrit_depth(self, r3_qutrit_circuit: Circuit) -> None: assert r3_qutrit_circuit.get_num_operations() == 10
def test_value(self, r6_qudit_circuit: Circuit) -> None: assert r6_qudit_circuit.get_num_operations() >= 0
def test_r3_con_depth(self, r3_qubit_constant_circuit: Circuit) -> None: assert r3_qubit_constant_circuit.get_num_operations() == 25