Exemplo n.º 1
0
 def test_empty(self) -> None:
     circuit = Circuit(1)
     assert len(circuit.get_params()) == 0
     circuit = Circuit(4)
     assert len(circuit.get_params()) == 0
     circuit = Circuit(4, [2, 3, 4, 5])
     assert len(circuit.get_params()) == 0
Exemplo n.º 2
0
 def test_empty(self) -> None:
     circuit = Circuit(1)
     assert circuit.get_parallelism() == 0
     circuit = Circuit(4)
     assert circuit.get_parallelism() == 0
     circuit = Circuit(4, [2, 3, 4, 5])
     assert circuit.get_parallelism() == 0
Exemplo n.º 3
0
 def test_empty(self) -> None:
     circuit = Circuit(1)
     assert circuit.get_num_cycles() == 0
     circuit = Circuit(4)
     assert circuit.get_num_cycles() == 0
     circuit = Circuit(4, [2, 3, 4, 5])
     assert circuit.get_num_cycles() == 0
Exemplo n.º 4
0
 def test_empty(self) -> None:
     circuit = Circuit(1)
     assert circuit.get_depth() == 0
     circuit = Circuit(4)
     assert circuit.get_depth() == 0
     circuit = Circuit(4, [2, 3, 4, 5])
     assert circuit.get_depth() == 0
Exemplo n.º 5
0
 def test_empty(self) -> None:
     circuit = Circuit(1)
     assert circuit.is_qubit_only()
     circuit = Circuit(4)
     assert circuit.is_qubit_only()
     circuit = Circuit(4, [2, 2, 3, 3])
     assert not circuit.is_qubit_only()
Exemplo n.º 6
0
 def test_empty(self) -> None:
     circuit = Circuit(1)
     assert circuit.get_dim() == 2
     circuit = Circuit(4)
     assert circuit.get_dim() == 16
     circuit = Circuit(4, [2, 2, 3, 3])
     assert circuit.get_dim() == 36
Exemplo n.º 7
0
 def test_empty(self) -> None:
     circuit = Circuit(1)
     assert circuit.get_size() == 1
     circuit = Circuit(4)
     assert circuit.get_size() == 4
     circuit = Circuit(4, [2, 2, 3, 3])
     assert circuit.get_size() == 4
Exemplo n.º 8
0
 def test_empty(self) -> None:
     circuit = Circuit(1)
     assert circuit.is_constant()
     circuit = Circuit(4)
     assert circuit.is_constant()
     circuit = Circuit(4, [2, 2, 3, 3])
     assert circuit.is_constant()
Exemplo n.º 9
0
 def test_empty(self) -> None:
     circuit = Circuit(1)
     assert not circuit.is_parameterized()
     circuit = Circuit(4)
     assert not circuit.is_parameterized()
     circuit = Circuit(4, [2, 2, 3, 3])
     assert not circuit.is_parameterized()
Exemplo n.º 10
0
class TestIsDifferentiable:
    """This tests `circuit.is_differentiable`."""
    def test_type(self, r6_qudit_circuit: Circuit) -> None:
        assert isinstance(r6_qudit_circuit.is_differentiable(), bool)

    def test_value(self, gate: Gate) -> None:
        circuit = Circuit(gate.get_size(), gate.get_radixes())
        assert circuit.is_differentiable()

        circuit.append_gate(gate, list(range(gate.get_size())))
        if isinstance(gate, DifferentiableUnitary):
            assert circuit.is_differentiable()
        else:
            assert not circuit.is_differentiable()

    @pytest.mark.parametrize(
        'circuit',
        [
            Circuit(1),
            Circuit(4),
            Circuit(4, [2, 3, 4, 5]),
        ],
    )
    def test_empty(self, circuit: Circuit) -> None:
        assert circuit.is_differentiable()
Exemplo n.º 11
0
 def test_type_valid_2(self) -> None:
     circuit = Circuit(4, [2, 2, 3, 3])
     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
Exemplo n.º 12
0
 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)
Exemplo n.º 13
0
 def test_removing_gate(self) -> None:
     circuit = Circuit(1)
     assert len(circuit.get_gate_set()) == 0
     circuit.append_gate(U3Gate(), [0])
     circuit.append_gate(XGate(), [0])
     circuit.append_gate(ZGate(), [0])
     circuit.append_gate(TGate(), [0])
     assert len(circuit.get_gate_set()) == 4
     assert U3Gate() in circuit.get_gate_set()
     assert XGate() in circuit.get_gate_set()
     assert ZGate() in circuit.get_gate_set()
     assert TGate() in circuit.get_gate_set()
     circuit.remove(TGate())
     assert len(circuit.get_gate_set()) == 3
     assert U3Gate() in circuit.get_gate_set()
     assert XGate() in circuit.get_gate_set()
     assert ZGate() in circuit.get_gate_set()
     circuit.remove(XGate())
     assert len(circuit.get_gate_set()) == 2
     assert U3Gate() in circuit.get_gate_set()
     assert ZGate() in circuit.get_gate_set()
     circuit.remove(ZGate())
     assert len(circuit.get_gate_set()) == 1
     assert U3Gate() in circuit.get_gate_set()
     circuit.remove(U3Gate())
     assert len(circuit.get_gate_set()) == 0
Exemplo n.º 14
0
 def test_qudit(self) -> None:
     circuit = Circuit(3, [2, 3, 3])
     assert len(circuit.get_gate_set()) == 0
     circuit.append_gate(U3Gate(), [0])
     assert len(circuit.get_gate_set()) == 1
     assert U3Gate() in circuit.get_gate_set()
     circuit.append_gate(XGate(), [0])
     assert len(circuit.get_gate_set()) == 2
     assert U3Gate() in circuit.get_gate_set()
     assert XGate() in circuit.get_gate_set()
     circuit.append_gate(ZGate(), [0])
     assert len(circuit.get_gate_set()) == 3
     assert U3Gate() in circuit.get_gate_set()
     assert XGate() in circuit.get_gate_set()
     assert ZGate() in circuit.get_gate_set()
     circuit.append_gate(TGate(), [0])
     assert len(circuit.get_gate_set()) == 4
     assert U3Gate() in circuit.get_gate_set()
     assert XGate() in circuit.get_gate_set()
     assert ZGate() in circuit.get_gate_set()
     assert TGate() in circuit.get_gate_set()
     circuit.append_gate(CSUMGate(), [1, 2])
     assert len(circuit.get_gate_set()) == 5
     assert U3Gate() in circuit.get_gate_set()
     assert XGate() in circuit.get_gate_set()
     assert ZGate() in circuit.get_gate_set()
     assert TGate() in circuit.get_gate_set()
     assert CSUMGate() in circuit.get_gate_set()
Exemplo n.º 15
0
    def test_two_qubit_2(self) -> None:
        circuit = Circuit(4)
        assert len(circuit.get_coupling_graph()) == 0

        circuit.append_gate(CNOTGate(), [0, 1])
        circuit.append_gate(CNOTGate(), [1, 2])
        circuit.append_gate(CNOTGate(), [2, 3])
        cgraph = circuit.get_coupling_graph()
        assert len(cgraph) == 3
        assert (0, 1) in cgraph
        assert (1, 2) in cgraph
        assert (2, 3) in cgraph

        circuit.append_gate(CNOTGate(), [2, 3])
        circuit.append_gate(CNOTGate(), [1, 2])
        circuit.append_gate(CNOTGate(), [0, 1])
        cgraph = circuit.get_coupling_graph()
        assert len(cgraph) == 3
        assert (0, 1) in cgraph
        assert (1, 2) in cgraph
        assert (2, 3) in cgraph

        circuit.append_gate(CNOTGate(), [0, 2])
        circuit.append_gate(CNOTGate(), [3, 0])
        cgraph = circuit.get_coupling_graph()
        assert len(cgraph) == 5
        assert (0, 1) in cgraph
        assert (1, 2) in cgraph
        assert (2, 3) in cgraph
        assert (0, 2) in cgraph
        assert (0, 3) in cgraph
Exemplo n.º 16
0
    def test_multi_qudit_2(self, gen_random_utry_np: Any) -> None:
        circuit = Circuit(6, [2, 2, 2, 3, 3, 3])
        assert len(circuit.get_coupling_graph()) == 0

        three_qubit_gate = ConstantUnitaryGate(
            gen_random_utry_np(12),
            [2, 2, 3],
        )
        circuit.append_gate(three_qubit_gate, [0, 1, 3])
        cgraph = circuit.get_coupling_graph()
        assert len(cgraph) == 3
        assert (0, 1) in cgraph
        assert (1, 3) in cgraph
        assert (0, 3) in cgraph

        circuit.append_gate(CNOTGate(), [1, 2])
        cgraph = circuit.get_coupling_graph()
        assert len(cgraph) == 4
        assert (0, 1) in cgraph
        assert (1, 3) in cgraph
        assert (0, 3) in cgraph
        assert (1, 2) in cgraph

        circuit.append_gate(CSUMGate(), [4, 5])
        cgraph = circuit.get_coupling_graph()
        assert len(cgraph) == 5
        assert (0, 1) in cgraph
        assert (1, 3) in cgraph
        assert (0, 3) in cgraph
        assert (1, 2) in cgraph
        assert (4, 5) in cgraph
Exemplo n.º 17
0
def test_full_pauli_gate() -> None:
    circuit = Circuit(3)
    circuit.append_gate(PauliGate(3), [0, 1, 2])
    cost = HilbertSchmidtResiduals(circuit,
                                   UnitaryMatrix(unitary_group.rvs(8)))
    circuit.minimize(cost)
    assert cost.get_cost(circuit.get_params()) < 1e-6
Exemplo n.º 18
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())
Exemplo n.º 19
0
 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
Exemplo n.º 20
0
 def test_return_type(self, an_int: int) -> None:
     circuit = Circuit(4, [2, 2, 3, 3])
     assert isinstance(
         circuit.is_point_in_range(
             (an_int, an_int),
         ), (bool, np.bool_),
     )
Exemplo n.º 21
0
 def test_empty(self) -> None:
     circuit = Circuit(1)
     assert len(circuit.get_radixes()) == 1
     assert circuit.get_radixes()[0] == 2
     circuit = Circuit(4)
     assert len(circuit.get_radixes()) == 4
     assert circuit.get_radixes()[0] == 2
     assert circuit.get_radixes()[1] == 2
     assert circuit.get_radixes()[2] == 2
     assert circuit.get_radixes()[3] == 2
     circuit = Circuit(4, [2, 2, 3, 3])
     assert len(circuit.get_radixes()) == 4
     assert circuit.get_radixes()[0] == 2
     assert circuit.get_radixes()[1] == 2
     assert circuit.get_radixes()[2] == 3
     assert circuit.get_radixes()[3] == 3
Exemplo n.º 22
0
 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()
Exemplo n.º 23
0
 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])
Exemplo n.º 24
0
 def test_append_gate_5(self) -> None:
     circuit = Circuit(4)
     circuit.append_gate(CNOTGate(), [0, 1])
     circuit.append_gate(CNOTGate(), [1, 2])
     circuit.append_gate(CNOTGate(), [2, 3])
     circuit.insert_qudit(4)
     circuit.append_gate(CNOTGate(), [0, 3])
     assert circuit.get_size() == 5
     assert len(circuit.get_radixes()) == 5
     assert circuit.get_radixes().count(2) == 5
     assert circuit[3, 0].gate == CNOTGate()
     assert circuit[3, 0].location == (0, 3)
     assert circuit[0, 1].gate == CNOTGate()
     assert circuit[0, 1].location == (0, 1)
     assert circuit[1, 2].gate == CNOTGate()
     assert circuit[1, 2].location == (1, 2)
     assert circuit[2, 3].gate == CNOTGate()
     assert circuit[2, 3].location == (2, 3)
     assert circuit[3, 3].gate == CNOTGate()
     assert circuit[3, 3].location == (0, 3)
     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, 2].gate == CNOTGate()
     assert circuit[2, 2].location == (2, 3)
Exemplo n.º 25
0
    def test_run(self) -> None:
        """Test run with a linear topology."""
        #     0  1  2  3  4        #########
        # 0 --o-----o--P--P--    --#-o---o-#-----#######--
        # 1 --x--o--x--o-----    --#-x-o-x-#######-o---#--
        # 2 -----x--o--x--o-- => --#---x---#---o-#-x-o-#--
        # 3 --o--P--x--P--x--    --#########-o-x-#---x-#--
        # 4 --x-----------P--    ----------#-x---#######--
        #                                  #######

        num_q = 5
        circ = Circuit(num_q)
        circ.append_gate(CNOTGate(), [0, 1])
        circ.append_gate(CNOTGate(), [3, 4])
        circ.append_gate(CNOTGate(), [1, 2])
        circ.append_gate(CNOTGate(), [0, 1])
        circ.append_gate(CNOTGate(), [2, 3])
        circ.append_gate(CNOTGate(), [1, 2])
        circ.append_gate(CNOTGate(), [2, 3])
        utry = circ.get_unitary()
        ScanPartitioner(3).run(circ, {})

        assert len(circ) == 3
        assert all(isinstance(op.gate, CircuitGate) for op in circ)
        placeholder_gate = TaggedGate(IdentityGate(1), '__fold_placeholder__')
        assert all(
            op.gate._circuit.count(placeholder_gate) == 0
            for op in circ)  # type: ignore  # noqa
        assert circ.get_unitary() == utry
        for cycle_index in range(circ.get_num_cycles()):
            assert not circ._is_cycle_idle(cycle_index)
Exemplo n.º 26
0
def swap_circuit() -> Circuit:
    """Provides a swap implemented with 3 cnots as a circuit fixture."""
    circuit = Circuit(2)
    circuit.append_gate(CNOTGate(), [0, 1])
    circuit.append_gate(CNOTGate(), [1, 0])
    circuit.append_gate(CNOTGate(), [0, 1])
    return circuit
Exemplo n.º 27
0
    def run(self, circuit: Circuit, data: dict[str, Any]) -> None:
        """Perform the pass's operation, see BasePass for more info."""

        # Check data for windows markers
        if 'window_markers' not in data:
            _logger.warning('Did not find any window markers.')
            return

        window_markers = data['window_markers']
        _logger.debug('Found window_markers: %s.' % str(window_markers))

        if not is_iterable(window_markers):
            _logger.debug('Invalid type for window_markers.')
            return

        if not all(is_integer(marker) for marker in window_markers):
            _logger.debug('Invalid type for window_markers.')
            return

        # Resynthesis each window
        index_shift = 0
        for marker in window_markers:
            marker -= index_shift

            # Slice window
            begin_cycle = int(marker - self.window_size // 2)
            end_cycle = int(marker + np.ceil(self.window_size / 2))

            if begin_cycle < 0:
                begin_cycle = 0

            if end_cycle > circuit.get_num_cycles():
                end_cycle = circuit.get_num_cycles() - 1

            window = Circuit(circuit.get_size(), circuit.get_radixes())
            window.extend(circuit[begin_cycle:end_cycle])

            _logger.info(
                'Resynthesizing window from cycle '
                f'{begin_cycle} to {end_cycle}.', )

            # Synthesize
            utry = window.get_unitary()
            new_window = self.synthesispass.synthesize(utry, data)

            # Replace
            if self.replace_filter(new_window, window):
                _logger.debug('Replacing window with synthesized circuit.')

                actual_window_size = end_cycle - begin_cycle
                for _ in range(actual_window_size):
                    circuit.pop_cycle(begin_cycle)

                circuit.insert_circuit(
                    begin_cycle,
                    new_window,
                    list(range(circuit.get_size())),
                )

                index_shift += actual_window_size - new_window.get_num_cycles()
Exemplo n.º 28
0
    def test_random_batch_remove(self) -> None:
        num_gates = 200
        num_q = 10

        circ = Circuit(num_q)
        for _ in range(num_gates):
            a = randint(0, num_q - 1)
            b = randint(0, num_q - 1)
            if a == b:
                b = (b + 1) % num_q
            circ.append_gate(CNOTGate(), [a, b])

        ScanPartitioner(2).run(circ, {})

        points = []
        ops = []
        for cycle, op in circ.operations_with_cycles():
            point = (cycle, op.location[0])
            ops.append(Operation(CNOTGate(), op.location))
            points.append(point)

        circ.batch_replace(points, ops)

        for op in circ:
            assert isinstance(op.gate, CNOTGate)
Exemplo n.º 29
0
    def gen_initial_layer(
        self,
        target: UnitaryMatrix | StateVector,
        data: dict[str, Any],
    ) -> Circuit:
        """
        Generate the initial layer, see LayerGenerator for more.

        Raises:
            ValueError: If `target` has a radix mismatch with
                `self.initial_layer_gate`.
        """

        if not isinstance(target, (UnitaryMatrix, StateVector)):
            raise TypeError(
                'Expected unitary or state, got %s.' % type(target), )

        for radix in target.get_radixes():
            if radix != self.initial_layer_gate.get_radixes()[0]:
                raise ValueError(
                    'Radix mismatch between target and initial_layer_gate.', )

        init_circuit = Circuit(target.get_size(), target.get_radixes())
        for i in range(init_circuit.get_size()):
            init_circuit.append_gate(self.initial_layer_gate, [i])
        return init_circuit
Exemplo n.º 30
0
    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