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_true_neg(self, cycle_index: int) -> None: circuit = Circuit(1) circuit.append_gate(HGate(), [0]) circuit.append_gate(HGate(), [0]) circuit.append_gate(HGate(), [0]) circuit.append_gate(HGate(), [0]) circuit.append_gate(HGate(), [0]) assert circuit.is_cycle_in_range(cycle_index)
def test_false_pos(self, cycle_index: int) -> None: circuit = Circuit(1) circuit.append_gate(HGate(), [0]) circuit.append_gate(HGate(), [0]) circuit.append_gate(HGate(), [0]) circuit.append_gate(HGate(), [0]) circuit.append_gate(HGate(), [0]) assert not circuit.is_cycle_in_range(cycle_index)
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_false_pos(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 not circuit.is_point_in_range(point)
def test_index_invalid_2(self, cycle_index: int) -> None: circuit = Circuit(4, [2, 2, 3, 3]) circuit.append_gate(HGate(), [0]) circuit.append_gate(HGate(), [0]) circuit.append_gate(HGate(), [0]) try: circuit.pop_cycle(cycle_index) except IndexError: return except BaseException: assert False, 'Unexpected Exception.'
def test_single(self) -> None: circuit = Circuit(4, [2, 2, 2, 2]) assert circuit.is_qudit_idle(0) assert circuit.is_qudit_idle(1) assert circuit.is_qudit_idle(2) assert circuit.is_qudit_idle(3) circuit.append_gate(HGate(), [0]) assert not circuit.is_qudit_idle(0) assert circuit.is_qudit_idle(1) assert circuit.is_qudit_idle(2) assert circuit.is_qudit_idle(3) circuit.append_gate(HGate(), [1]) assert not circuit.is_qudit_idle(0) assert not circuit.is_qudit_idle(1) assert circuit.is_qudit_idle(2) assert circuit.is_qudit_idle(3) circuit.append_gate(HGate(), [2]) assert not circuit.is_qudit_idle(0) assert not circuit.is_qudit_idle(1) assert not circuit.is_qudit_idle(2) assert circuit.is_qudit_idle(3) circuit.append_gate(HGate(), [3]) assert not circuit.is_qudit_idle(0) assert not circuit.is_qudit_idle(1) assert not circuit.is_qudit_idle(2) assert not circuit.is_qudit_idle(3) circuit.pop((0, 0)) assert circuit.is_qudit_idle(0) assert not circuit.is_qudit_idle(1) assert not circuit.is_qudit_idle(2) assert not circuit.is_qudit_idle(3) circuit.pop((0, 1)) assert circuit.is_qudit_idle(0) assert circuit.is_qudit_idle(1) assert not circuit.is_qudit_idle(2) assert not circuit.is_qudit_idle(3) circuit.pop((0, 2)) assert circuit.is_qudit_idle(0) assert circuit.is_qudit_idle(1) assert circuit.is_qudit_idle(2) assert not circuit.is_qudit_idle(3) circuit.pop((0, 3)) assert circuit.is_qudit_idle(0) assert circuit.is_qudit_idle(1) assert circuit.is_qudit_idle(2) assert circuit.is_qudit_idle(3)
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_2(self) -> None: circuit = Circuit(2) circuit.append_gate(HGate(), [0]) circuit.append_gate(CNOTGate(), [0, 1]) assert circuit.point(HGate()) == (0, 0) assert circuit.point(CNOTGate()) == (1, 0) assert circuit.point(Operation(HGate(), [0])) == (0, 0) assert circuit.point(Operation(CNOTGate(), [0, 1])) == (1, 0) try: circuit.point(Operation(CNOTGate(), [1, 0])) except ValueError: return assert False, 'Should not have reached here.'
def test_example(self) -> None: circuit = Circuit(2) circuit.append_gate(HGate(), [0]) circuit.append_gate(XGate(), [0]) circuit.append_gate(ZGate(), [1]) assert not circuit.is_cycle_unoccupied(0, [0]) assert circuit.is_cycle_unoccupied(1, [1])
def test_example(self) -> None: circuit = Circuit(2) circuit.append_gate(HGate(), [0]) assert circuit.find_available_cycle([1]) == 0 circuit.append_gate(XGate(), [0]) circuit.append_gate(ZGate(), [1]) assert circuit.find_available_cycle([1]) == 1
def test_type_valid_2(self) -> None: circuit = Circuit(4, [2, 2, 3, 3]) try: circuit.extend([Operation(HGate(), [0])]) except TypeError: assert False, 'Unexpected TypeError.' except BaseException: return
def test_type_valid_2(self) -> None: circuit = Circuit(4, [2, 2, 3, 3]) try: circuit.append_gate(HGate(), [2]) except TypeError: assert False, 'Unexpected TypeError.' except BaseException: return
def test_type_valid_1(self) -> None: circuit = Circuit(1) try: circuit.append(Operation(HGate(), [0])) except TypeError: assert False, 'Unexpected TypeError.' except BaseException: return
def test_type_valid_1(self) -> None: circuit = Circuit(1) try: circuit.point(HGate()) except TypeError: assert False, 'Unexpected TypeError.' except BaseException: return
def test_get_gate_set(self, toffoli_circuit: Circuit) -> None: gate_set = toffoli_circuit.get_gate_set() assert isinstance(gate_set, set) assert len(gate_set) == 4 assert CNOTGate() in gate_set assert HGate() in gate_set assert TdgGate() in gate_set assert TGate() in gate_set
def toffoli_circuit() -> Circuit: """Provides a standard toffoli implemention.""" circuit = Circuit(3) circuit.append_gate(HGate(), [2]) circuit.append_gate(CNOTGate(), [1, 2]) circuit.append_gate(TdgGate(), [2]) circuit.append_gate(CNOTGate(), [0, 2]) circuit.append_gate(TGate(), [2]) circuit.append_gate(CNOTGate(), [1, 2]) circuit.append_gate(TdgGate(), [2]) circuit.append_gate(CNOTGate(), [0, 2]) circuit.append_gate(TGate(), [1]) circuit.append_gate(TGate(), [2]) circuit.append_gate(CNOTGate(), [0, 1]) circuit.append_gate(HGate(), [2]) circuit.append_gate(TGate(), [0]) circuit.append_gate(TdgGate(), [1]) circuit.append_gate(CNOTGate(), [0, 1]) return circuit
def test_type_valid_1(self) -> None: circuit = Circuit(1) circuit_to_add = Circuit(1) circuit_to_add.append_gate(HGate(), [0]) try: circuit.append_circuit(circuit_to_add, [0]) except TypeError: assert False, 'Unexpected TypeError.' except BaseException: return
def test_invalid_value_2(self) -> None: circuit = Circuit(2) circuit.append_gate(HGate(), [0]) circuit.append_gate(CNOTGate(), [0, 1]) try: circuit.point(XGate()) except ValueError: return assert False, 'Should not have reached here.'
def test_example(self) -> None: circuit = Circuit(1) opH = Operation(HGate(), [0]) circuit.append(opH) assert circuit.point(opH).__repr__( ) == 'CircuitPoint(cycle=0, qudit=0)' opX = Operation(XGate(), [0]) circuit.append(opX) assert circuit.point(opX).__repr__( ) == 'CircuitPoint(cycle=1, qudit=0)'
def test_return_type(self) -> 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.point(HGate()), CircuitPoint)
"""Provide a method to generate random invalid unitaries.""" return invalid_utry_gen # endregion # region Gates BQSKIT_GATES = [ CXGate(), CPIGate(), CSUMGate(), CNOTGate(), CYGate(), CZGate(), HGate(), IdentityGate(1), IdentityGate(2), IdentityGate(3), IdentityGate(4), ISwapGate(), # PermutationGate(), # TODO SGate(), SdgGate(), SqrtCNOTGate(), SwapGate(), SXGate(), SqrtXGate(), TGate(), TdgGate(), ConstantUnitaryGate(TOFFOLI), # TODO
class TestCheckValidOperation: """This tests `circuit.check_valid_operation`.""" @pytest.mark.parametrize( ('circuit', 'op'), [ (Circuit(1), Operation(HGate(), [0])), (Circuit(1), Operation(CNOTGate(), [0, 1])), (Circuit(1), Operation(CPIGate(), [2, 3])), (Circuit(4, [2, 2, 3, 3]), Operation(HGate(), [0])), (Circuit(4, [2, 2, 3, 3]), Operation(CNOTGate(), [0, 1])), (Circuit(4, [2, 2, 3, 3]), Operation(CPIGate(), [2, 3])), ], ) def test_type_valid(self, circuit: Circuit, op: Operation) -> None: try: circuit.check_valid_operation(op) except TypeError: assert False, 'Unexpected Exception.' except BaseException: return @pytest.mark.parametrize( ('circuit', 'op'), [ (Circuit(1), 'A'), (Circuit(1), 0), (Circuit(1), np.int64(1234)), (Circuit(4, [2, 2, 3, 3]), 'A'), (Circuit(4, [2, 2, 3, 3]), 0), (Circuit(4, [2, 2, 3, 3]), np.int64(1234)), ], ) def test_type_invalid(self, circuit: Circuit, op: Operation) -> None: try: circuit.check_valid_operation(op) except TypeError: return except BaseException: assert False, 'Unexpected Exception.' def test_location_mismatch_1(self, qubit_gate: Gate) -> None: circuit = Circuit(qubit_gate.get_size()) location = list(range(qubit_gate.get_size())) location[-1] += 1 params = [0] * qubit_gate.get_num_params() op = Operation(qubit_gate, location, params) try: circuit.check_valid_operation(op) except ValueError: return except BaseException: assert False, 'Unexpected Exception' assert False def test_location_mismatch_2(self, qutrit_gate: Gate) -> None: circuit = Circuit(qutrit_gate.get_size(), qutrit_gate.get_radixes()) location = list(range(qutrit_gate.get_size())) location[-1] += 1 params = [0] * qutrit_gate.get_num_params() op = Operation(qutrit_gate, location, params) try: circuit.check_valid_operation(op) except ValueError: return except BaseException: assert False, 'Unexpected Exception' assert False def test_radix_mismatch_1(self, qubit_gate: Gate) -> None: circuit = Circuit(qubit_gate.get_size(), [3] * qubit_gate.get_size()) location = list(range(qubit_gate.get_size())) params = [0] * qubit_gate.get_num_params() op = Operation(qubit_gate, location, params) try: circuit.check_valid_operation(op) except ValueError: return except BaseException: assert False, 'Unexpected Exception' assert False def test_radix_mismatch_2(self, qutrit_gate: Gate) -> None: circuit = Circuit(qutrit_gate.get_size()) location = list(range(qutrit_gate.get_size())) params = [0] * qutrit_gate.get_num_params() op = Operation(qutrit_gate, location, params) try: circuit.check_valid_operation(op) except ValueError: return except BaseException: assert False, 'Unexpected Exception' assert False def test_valid_1(self, gate: Gate) -> None: circuit = Circuit(gate.get_size(), gate.get_radixes()) location = list(range(gate.get_size())) params = [0] * gate.get_num_params() circuit.check_valid_operation(Operation(gate, location, params)) def test_valid_2(self, gate: Gate) -> None: circuit = Circuit(gate.get_size() + 2, (2, 2) + gate.get_radixes()) location = [x + 2 for x in list(range(gate.get_size()))] params = [0] * gate.get_num_params() circuit.check_valid_operation(Operation(gate, location, params)) def test_valid_3(self) -> None: circuit = Circuit(2, [3, 2]) gate = ConstantUnitaryGate(np.identity(6), [2, 3]) circuit.check_valid_operation(Operation(gate, [1, 0]))
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)'
def test_one_gate_1(self) -> None: circuit = Circuit(1) circuit.append_gate(HGate(), 0) ops = [op for op in CircuitIterator(circuit)] assert len(ops) == 1 assert ops[0].gate == HGate() # type: ignore