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_in1 = _random_operation(1)
        operation_in2 = _random_operation(1)
        operation_in3 = _random_operation(1)
        operation_in4 = _random_operation(1)
        operation_out1 = _random_operation(1)
        operation_out2 = _random_operation(1)
        operation_out3 = _random_operation(1)

        # define the transformation rule
        def perform_rule(operations_in):
            # check type and value for operations_in
            self.assertIs(type(operations_in), tuple)
            self.assertTrue(
                _elementwise_is(operations_in, [
                    operation_in1, operation_in2, operation_in3, operation_in4
                ]))

            # return the modified operations
            return [operation_out1, operation_out2, operation_out3]

        # initialize the PointTransformation
        transformation = transform.GroupTransformation(
            attention_circ=transform.AttentionCircuit(
                focus=[
                    operation_in1, operation_in2, operation_in3, operation_in4
                ],
                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_out3,
                operation_between1, operation_between2, operation_after
            ]))
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.GroupTransformation(circ, perform_rule, None)
Exemple #3
0
  def test_initializer_and_getters(self):
    # preparation work
    focus_in = [
        _random_operation(1, 2),
        _random_operation(2, 3),
        _random_operation(3, 4)
    ]
    context_in = transform.TransformationContext(
        circuit.Circuit(5, None),
        circuit.Circuit(5, None),
        circuit.Circuit(5, None)
    )
    locations_in = (0, 1, 2)
    perform_rule = lambda operation_in: [operation_in]
    rule_id = object()

    # initialize the PairTransformation
    transformation = transform.GroupTransformation(
        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 #4
0
 def transformations_from_scanner(self, scanner):
     # implements abstract method from parent class TransformationRule
     for attention_circ in scanner.local_groups():
         if self.accept(attention_circ.focus()):
             yield transform.GroupTransformation(attention_circ,
                                                 self.perform, self)