Exemplo n.º 1
0
    def test_swap_overlap_orthogonal(self):
        random_rotations = generate_random_rotation_batch(
            self.num_qubits_of_a_state, self.circuit_batch_size)

        circuit_input1 = tfq.convert_to_tensor(
            act_gate_batch_on_qubits(random_rotations,
                                     self.state1,
                                     as_circuits=True))
        circuit_input2 = tfq.convert_to_tensor(
            act_gate_batch_on_qubits(random_rotations,
                                     self.state2,
                                     as_circuits=True))
        circuit_input2 = tfq_utility_ops.tfq_append_circuit(
            tfq.convert_to_tensor(
                [cirq.Circuit([cirq.X.on_each(self.state2)])] *
                self.circuit_batch_size), circuit_input2)

        swap_layer: SWAPTestLayer = SWAPTestLayer(self.state1, self.state2)
        swap_test: tf.Tensor = swap_layer([circuit_input1, circuit_input2])

        output: tf.Tensor = tfq.layers.Expectation()(
            swap_test, operators=[cirq.Z(swap_layer.auxiliary_qubit)])
        print(output)
        self.assertTrue(
            tf_allclose(output,
                        tf.convert_to_tensor([[0.0]] *
                                             self.circuit_batch_size),
                        rtol=1e-5,
                        atol=1e-5))
Exemplo n.º 2
0
    def test_swap_overlap_orthogonal(self):
        random_rotations = [
            General1BitRotation(sympy.Symbol(f"theta_q{i}_1"),
                                sympy.Symbol(f"theta_q{i}_2"),
                                sympy.Symbol(f"theta_q{i}_3"))
            for i in range(self.num_qubits_of_a_state)
        ]

        circuit_input1 = tfq.convert_to_tensor([cirq.Circuit()] *
                                               self.circuit_batch_size)
        circuit_input2 = tfq.convert_to_tensor(
            [cirq.Circuit(cirq.X.on_each(self.state2))] *
            self.circuit_batch_size)

        swap_layer = SWAPTestOutputLayer(
            circuit_prepend_of_state1=act_gate_batch_on_qubits(
                [random_rotations], self.state1, as_circuits=True)[0],
            circuit_prepend_of_state2=act_gate_batch_on_qubits(
                [random_rotations], self.state2, as_circuits=True)[0],
            state1=self.state1,
            state2=self.state2)
        output: tf.Tensor = swap_layer([circuit_input1, circuit_input2])
        print(output)
        self.assertTrue(
            tf_allclose(output,
                        tf.convert_to_tensor([[0.0]] *
                                             self.circuit_batch_size),
                        rtol=1e-5,
                        atol=1e-5))
Exemplo n.º 3
0
    def test_swap_overlap_identical(self):
        random_rotations = generate_random_rotation_batch(
            self.num_qubits_of_a_state, self.circuit_batch_size)

        circuit_input1 = tfq.convert_to_tensor(
            act_gate_batch_on_qubits(random_rotations,
                                     self.state1,
                                     as_circuits=True))
        circuit_input2 = tfq.convert_to_tensor(
            act_gate_batch_on_qubits(random_rotations,
                                     self.state2,
                                     as_circuits=True))

        swap_layer: SWAPTestOutputLayer = SWAPTestOutputLayer(
            circuit_prepend_of_state1=cirq.Circuit(
                [cirq.rx(sympy.Symbol("theta"))(self.state1[0])]),
            circuit_prepend_of_state2=cirq.Circuit(
                [cirq.rx(sympy.Symbol("theta"))(self.state2[0])]),
            state1=self.state1,
            state2=self.state2)
        output: tf.Tensor = swap_layer([circuit_input1, circuit_input2])

        print(output)
        self.assertTrue(
            tf_allclose(output,
                        tf.convert_to_tensor([[1.0]] *
                                             self.circuit_batch_size),
                        rtol=1e-2))
Exemplo n.º 4
0
    def test_swap_overlap_random_rotations(self):
        circuit_input1 = tfq.convert_to_tensor(
            act_gate_batch_on_qubits(generate_random_rotation_batch(
                self.num_qubits_of_a_state, self.circuit_batch_size),
                                     self.state1,
                                     as_circuits=True))
        circuit_input2 = tfq.convert_to_tensor(
            [cirq.Circuit([cirq.I.on_each(*self.state2)])] *
            self.circuit_batch_size)

        swap_layer: SWAPTestLayer = SWAPTestLayer(self.state1, self.state2)
        swap_test: tf.Tensor = swap_layer([circuit_input1, circuit_input2])

        output: tf.Tensor = tfq.layers.Expectation()(
            swap_test, operators=[cirq.Z(swap_layer.auxiliary_qubit)])
        print(output)
Exemplo n.º 5
0
    def test_swap_overlap_random_rotations(self):
        circuit_input1 = tfq.convert_to_tensor(
            act_gate_batch_on_qubits(generate_random_rotation_batch(
                self.num_qubits_of_a_state, self.circuit_batch_size),
                                     self.state1,
                                     as_circuits=True))
        circuit_input2 = tfq.convert_to_tensor([cirq.Circuit()] *
                                               self.circuit_batch_size)

        swap_layer = SWAPTestOutputLayer(
            circuit_prepend_of_state1=cirq.Circuit(
                [cirq.rx(sympy.Symbol("theta1"))(self.state1[0])]),
            circuit_prepend_of_state2=cirq.Circuit(
                [cirq.rx(sympy.Symbol("theta2"))(self.state2[0])]),
            circuit_append=cirq.Circuit([
                cirq.rx(-sympy.Symbol("theta1"))(self.state1[0]),
                cirq.rx(-sympy.Symbol("theta2"))(self.state2[0])
            ]),
            state1=self.state1,
            state2=self.state2)
        output: tf.Tensor = swap_layer([circuit_input1, circuit_input2])
        print(output)
        self.assertEqual(output.shape, (5, 1))