def test_correctness_2(self) -> None: circuit = Circuit(2) circuit.append_gate(HGate(), [0]) circuit.append_gate(CNOTGate(), [0, 1]) assert circuit.get_operation((0, 0)).gate == HGate() assert circuit.get_operation((1, 0)).gate == CNOTGate() assert circuit.get_operation((1, 1)).gate == CNOTGate()
def test_type_invalid_3(self, not_an_int: Any) -> None: circuit = Circuit(1) try: circuit.get_operation((not_an_int, not_an_int)) except TypeError: return except BaseException: assert False, 'Unexpected Exception.'
def test_type_valid_4(self, an_int: int) -> None: circuit = Circuit(4, [2, 2, 3, 3]) try: circuit.get_operation(CircuitPoint(an_int, an_int)) except TypeError: assert False, 'Unexpected TypeError.' except BaseException: return
def test_index_error_out_of_bounds(self, point: CircuitPointLike) -> None: circuit = Circuit(5) for i in range(5): circuit.append_gate(HGate(), [0]) circuit.append_gate(HGate(), [1]) circuit.append_gate(HGate(), [2]) circuit.append_gate(HGate(), [3]) circuit.append_gate(HGate(), [4]) try: circuit.get_operation(point) except IndexError: return assert False, 'Should not have reached here.'
def test_correctness_1(self, r6_qudit_circuit: Circuit) -> None: for x in range(r6_qudit_circuit.get_num_cycles()): for y in range(r6_qudit_circuit.get_size()): op = r6_qudit_circuit._circuit[x][y] if op is not None: point = r6_qudit_circuit.point(op, (x, y)) assert r6_qudit_circuit.get_operation(point) is op point = r6_qudit_circuit.point(op, (x, y), (x, y)) assert r6_qudit_circuit.get_operation(point) is op point = r6_qudit_circuit.point(op.gate, (x, y)) assert r6_qudit_circuit.get_operation(point) is op point = r6_qudit_circuit.point(op.gate, (x, y), (x, y)) assert r6_qudit_circuit.get_operation(point) is op
def test_correctness_1(self, r6_qudit_circuit: Circuit) -> None: for x in range(r6_qudit_circuit.get_num_cycles()): for y in range(r6_qudit_circuit.get_size()): correct = r6_qudit_circuit._circuit[x][y] if correct is not None: assert correct is r6_qudit_circuit.get_operation((x, y)) else: try: r6_qudit_circuit.get_operation((x, y)) except IndexError: pass except BaseException: assert False, 'Unexpected exception.'
def test_return_type(self, point: CircuitPointLike) -> None: circuit = Circuit(5) for i in range(5): circuit.append_gate(HGate(), [0]) circuit.append_gate(HGate(), [1]) circuit.append_gate(HGate(), [2]) circuit.append_gate(HGate(), [3]) circuit.append_gate(HGate(), [4]) assert isinstance(circuit.get_operation(point), Operation)
def test_batch_replace(self) -> None: circ = Circuit(4) op_1a = Operation(CNOTGate(), [0, 1]) op_2a = Operation(CNOTGate(), [2, 3]) op_3a = Operation(CNOTGate(), [1, 2]) op_4a = Operation(CNOTGate(), [0, 1]) op_5a = Operation(CNOTGate(), [0, 1]) op_6a = Operation(CNOTGate(), [2, 3]) list_a = [op_1a, op_2a, op_3a, op_4a, op_5a, op_6a] op_1b = Operation(CNOTGate(), [1, 0]) op_2b = Operation(CNOTGate(), [3, 2]) op_3b = Operation(CNOTGate(), [2, 1]) op_4b = Operation(CNOTGate(), [1, 0]) op_5b = Operation(CNOTGate(), [1, 0]) op_6b = Operation(CNOTGate(), [3, 2]) list_b = [op_1b, op_2b, op_3b, op_4b, op_5b, op_6b] for op in list_a: circ.append(op) assert circ.get_operation((0, 0), ) == op_1a and circ.get_operation( (0, 1), ) == op_1a assert circ.get_operation((0, 2), ) == op_2a and circ.get_operation( (0, 3), ) == op_2a assert circ.get_operation((1, 1), ) == op_3a and circ.get_operation( (1, 2), ) == op_3a assert circ.get_operation((2, 0), ) == op_4a and circ.get_operation( (2, 1), ) == op_4a assert circ.get_operation((2, 2), ) == op_6a and circ.get_operation( (2, 3), ) == op_6a assert circ.get_operation((3, 0), ) == op_5a and circ.get_operation( (3, 1), ) == op_5a for i in range(4): for j in range(4): print(f'({i},{j}): {circ._circuit[i][j]}') points = [(0, 0), (0, 2), (1, 1), (2, 0), (3, 1), (2, 3)] new_ops = list_b circ.batch_replace(points, new_ops) assert circ.get_operation((0, 0), ) == op_1b and circ.get_operation( (0, 1), ) == op_1b assert circ.get_operation((0, 2), ) == op_2b and circ.get_operation( (0, 3), ) == op_2b assert circ.get_operation((1, 1), ) == op_3b and circ.get_operation( (1, 2), ) == op_3b assert circ.get_operation((2, 0), ) == op_4b and circ.get_operation( (2, 1), ) == op_4b assert circ.get_operation((2, 2), ) == op_6b and circ.get_operation( (2, 3), ) == op_6b assert circ.get_operation((3, 0), ) == op_5b and circ.get_operation( (3, 1), ) == op_5b
def test_example(self) -> None: circuit = Circuit(2) circuit.append_gate(HGate(), [0]) circuit.append_gate(CNOTGate(), [0, 1]) circuit.get_operation((1, 0)).__repr__() == 'CNOTGate@(0,1)'