def test_initializer_and_getters(self):
        # preparation work
        focus_in = [_random_operation(1, 2)]
        context_in = transform.TransformationContext(circuit.Circuit(5, None),
                                                     circuit.Circuit(5, None),
                                                     circuit.Circuit(5, None))
        locations_in = (0, )
        perform_rule = lambda operation_in: [operation_in]
        rule_id = object()

        # initialize the PointTransformation
        transformation = transform.PointTransformation(
            attention_circ=transform.AttentionCircuit(focus=focus_in,
                                                      context=context_in,
                                                      locations=locations_in),
            perform_rule=perform_rule,
            rule_id=rule_id)

        # check type and value transformation.focus()
        focus_out = transformation.focus()
        self.assertIs(type(focus_out), tuple)
        self.assertTrue(_elementwise_is(focus_out, focus_in))

        # check transformation.context()
        self.assertIs(transformation.context(), context_in)

        # check type and value transformation.locations()
        locations_out = transformation.locations()
        self.assertIs(type(locations_out), tuple)
        self.assertTrue(_elementwise_is(locations_out, locations_in))

        # check transformation.context()
        self.assertIs(transformation.rule_id(), rule_id)
Exemple #2
0
  def test_initializer_circ_type_error(self):
    circ = circuit.Circuit(5, None)
    perform_rule = lambda operation_in: [operation_in]

    with self.assertRaisesRegex(
        TypeError,
        r'attention_circ is not an AttentionCircuit \(found type: Circuit\)'):
      transform.PointTransformation(circ, perform_rule, None)
Exemple #3
0
  def transformations_from_scanner(self, scanner):
    # implements abstract method from parent class TransformationRule

    for attention_circ in scanner.single_operations():
      if len(attention_circ) != 1:
        raise RuntimeError()  # cmp. TODO at the beginning of this file
      operation, = attention_circ.focus()
      if self.accept(operation):
        yield transform.PointTransformation(attention_circ, self.perform, self)
    def test_initializer_focus_size_error(self):
        attention_circ = transform.AttentionCircuit(
            focus=[_random_operation(1),
                   _random_operation(3)],
            context=transform.TransformationContext(circuit.Circuit(5, None),
                                                    circuit.Circuit(5, None),
                                                    circuit.Circuit(5, None)))
        perform_rule = lambda operation_in: [operation_in]

        with self.assertRaisesRegex(
                ValueError,
                r'focus of attention_circ for PointTransformation must have size 1'
                r' \(found: 2\)'):
            transform.PointTransformation(attention_circ, perform_rule, None)
    def test_perform(self):
        # preparation work
        num_qubits = 5

        operation_before1 = _random_operation(0)
        operation_before2 = _random_operation(1)
        operation_before3 = _random_operation(2)
        operation_between1 = _random_operation(0)
        operation_between2 = _random_operation(1)
        operation_after = _random_operation(1)

        operation_in = _random_operation(1)
        operation_out1 = _random_operation(1)
        operation_out2 = _random_operation(1)

        # define the transformation rule
        def perform_rule(operation):
            # check operation
            self.assertIs(operation, operation_in)

            # return the modified operations
            return [operation_out1, operation_out2]

        # initialize the PointTransformation
        transformation = transform.PointTransformation(
            attention_circ=transform.AttentionCircuit(
                focus=[operation_in],
                context=transform.TransformationContext(
                    circuit.Circuit(num_qubits, [
                        operation_before1, operation_before2, operation_before3
                    ]),
                    circuit.Circuit(num_qubits,
                                    [operation_between1, operation_between2]),
                    circuit.Circuit(num_qubits, [operation_after]))),
            perform_rule=perform_rule,
            rule_id=None)

        # call the method to be tested
        circ_out = transformation.perform()

        # check type for circ_out
        self.assertIs(type(circ_out), circuit.Circuit)

        # check the value for circ_out
        self.assertTrue(
            _elementwise_is(circ_out.get_operation_sequence(), [
                operation_before1, operation_before2, operation_before3,
                operation_out1, operation_out2, operation_between1,
                operation_between2, operation_after
            ]))