Пример #1
0
    def test_negation_operator(self):
        """ negation operator test """
        paulis = [
            Pauli(x) for x in ['IXYZ', 'XXZY', 'IIZZ', 'XXYY', 'ZZXX', 'YYYY']
        ]
        coeffs = [0.2, 0.6, 0.8, -0.2, -0.6, -0.8]
        op1 = WeightedPauliOperator.from_list(paulis, coeffs)
        coeffs = [-0.2, -0.6, -0.8, 0.2, 0.6, 0.8]
        op2 = WeightedPauliOperator.from_list(paulis, coeffs)

        self.assertNotEqual(op1, op2)
        self.assertEqual(op1, -op2)
        self.assertEqual(-op1, op2)
        op1 = op1 * -1.0
        self.assertEqual(op1, op2)
Пример #2
0
    def test_simplify(self):
        """ simplify test """
        pauli_a = 'IXYZ'
        pauli_b = 'IXYZ'
        coeff_a = 0.5
        coeff_b = -0.5
        pauli_term_a = [coeff_a, Pauli(pauli_a)]
        pauli_term_b = [coeff_b, Pauli(pauli_b)]
        op_a = WeightedPauliOperator(paulis=[pauli_term_a])
        op_b = WeightedPauliOperator(paulis=[pauli_term_b])
        new_op = op_a + op_b
        new_op.simplify()

        self.assertEqual(0, len(new_op.paulis),
                         "{}".format(new_op.print_details()))
        self.assertTrue(new_op.is_empty())

        paulis = [
            Pauli(x) for x in ['IXYZ', 'XXZY', 'IIZZ', 'XXYY', 'ZZXX', 'YYYY']
        ]
        coeffs = [0.2, 0.6, 0.8, -0.2, -0.6, -0.8]
        op1 = WeightedPauliOperator.from_list(paulis, coeffs)

        for i, pauli in enumerate(paulis):
            tmp_op = WeightedPauliOperator(paulis=[[-coeffs[i], pauli]])
            op1 += tmp_op
            op1.simplify()
            self.assertEqual(len(paulis) - (i + 1), len(op1.paulis))
Пример #3
0
    def test_equal_operator(self):
        """ equal operator test """
        paulis = [
            Pauli(x) for x in ['IXYZ', 'XXZY', 'IIZZ', 'XXYY', 'ZZXX', 'YYYY']
        ]
        coeffs = [0.2, 0.6, 0.8, -0.2, -0.6, -0.8]
        op1 = WeightedPauliOperator.from_list(paulis, coeffs)

        coeffs = [0.2, 0.6, 0.8, -0.2, -0.6, -0.8]
        op2 = WeightedPauliOperator.from_list(paulis, coeffs)

        coeffs = [-0.2, -0.6, -0.8, 0.2, 0.6, 0.8]
        op3 = WeightedPauliOperator.from_list(paulis, coeffs)

        coeffs = [-0.2, 0.6, 0.8, -0.2, -0.6, -0.8]
        op4 = WeightedPauliOperator.from_list(paulis, coeffs)

        self.assertEqual(op1, op2)
        self.assertNotEqual(op1, op3)
        self.assertNotEqual(op1, op4)
        self.assertNotEqual(op3, op4)
Пример #4
0
    def setUp(self):
        super().setUp()
        seed = 0
        aqua_globals.random_seed = seed

        self.num_qubits = 2
        m_size = np.power(2, self.num_qubits)
        matrix = aqua_globals.random.random((m_size, m_size))
        self.mat_op = MatrixOperator(matrix=matrix)
        paulis = [Pauli("".join(pauli_label))
                  for pauli_label in itertools.product('IXYZ', repeat=self.num_qubits)]
        weights = aqua_globals.random.random(len(paulis))
        self.pauli_op = WeightedPauliOperator.from_list(paulis, weights)
Пример #5
0
 def test_from_to_file(self):
     """ from to file test """
     paulis = [
         Pauli(x) for x in ['IXYZ', 'XXZY', 'IIZZ', 'XXYY', 'ZZXX', 'YYYY']
     ]
     weights = [
         0.2 + -1j * 0.8, 0.6 + -1j * 0.6, 0.8 + -1j * 0.2,
         -0.2 + -1j * 0.8, -0.6 - -1j * 0.6, -0.8 - -1j * 0.2
     ]
     op = WeightedPauliOperator.from_list(paulis, weights)
     file_path = self._get_resource_path('temp_op.json')
     op.to_file(file_path)
     self.assertTrue(os.path.exists(file_path))
     try:
         load_op = WeightedPauliOperator.from_file(file_path)
         self.assertEqual(op, load_op)
     finally:
         os.remove(file_path)
    def setUp(self):
        super().setUp()
        seed = 1
        aqua_globals.random_seed = seed

        self.num_qubits = 3
        paulis = [Pauli.from_label(pauli_label)
                  for pauli_label in itertools.product('IXYZ', repeat=self.num_qubits)]
        weights = aqua_globals.random.random(len(paulis))
        self.qubit_op = WeightedPauliOperator.from_list(paulis, weights)
        self.var_form = EfficientSU2(self.qubit_op.num_qubits, reps=1)

        qasm_simulator = BasicAer.get_backend('qasm_simulator')
        self.quantum_instance_qasm = QuantumInstance(qasm_simulator, shots=65536,
                                                     seed_simulator=seed, seed_transpiler=seed)

        statevector_simulator = BasicAer.get_backend('statevector_simulator')
        self.quantum_instance_statevector = \
            QuantumInstance(statevector_simulator, shots=1,
                            seed_simulator=seed, seed_transpiler=seed)
    def test_unsorted_grouping(self):
        """Test with normal grouping approach."""

        num_qubits = 4
        paulis = [Pauli.from_label(pauli_label)
                  for pauli_label in itertools.product('IXYZ', repeat=num_qubits)]
        weights = aqua_globals.random.random(len(paulis))
        op = WeightedPauliOperator.from_list(paulis, weights)
        grouped_op = op_converter.to_tpb_grouped_weighted_pauli_operator(
            op, TPBGroupedWeightedPauliOperator.unsorted_grouping)

        for g_p in grouped_op.paulis:
            passed = False
            for pauli in op.paulis:
                if pauli[1] == g_p[1]:
                    passed = pauli[0] == g_p[0]
                    break
            self.assertTrue(passed,
                            "non-existed paulis in grouped_paulis: {}".format(g_p[1].to_label()))

        self.assertGreaterEqual(len(op.basis), len(grouped_op.basis))
Пример #8
0
    def test_chop_real(self):
        """ chop real test """
        paulis = [
            Pauli(x) for x in ['IXYZ', 'XXZY', 'IIZZ', 'XXYY', 'ZZXX', 'YYYY']
        ]
        coeffs = [0.2, 0.6, 0.8, -0.2, -0.6, -0.8]
        op = WeightedPauliOperator.from_list(paulis, coeffs)
        ori_op = op.copy()

        for threshold, num_paulis in zip([0.4, 0.7, 0.9], [4, 2, 0]):
            op = ori_op.copy()
            op1 = op.chop(threshold=threshold, copy=True)
            self.assertEqual(len(op.paulis), 6,
                             "\n{}".format(op.print_details()))
            self.assertEqual(len(op1.paulis), num_paulis,
                             "\n{}".format(op1.print_details()))

            op1 = op.chop(threshold=threshold, copy=False)
            self.assertEqual(len(op.paulis), num_paulis,
                             "\n{}".format(op.print_details()))
            self.assertEqual(len(op1.paulis), num_paulis,
                             "\n{}".format(op1.print_details()))
    def test_sorted_grouping(self):
        """Test with color grouping approach."""
        num_qubits = 2
        paulis = [Pauli.from_label(pauli_label)
                  for pauli_label in itertools.product('IXYZ', repeat=num_qubits)]
        weights = aqua_globals.random.random(len(paulis))
        op = WeightedPauliOperator.from_list(paulis, weights)
        grouped_op = op_converter.to_tpb_grouped_weighted_pauli_operator(
            op, TPBGroupedWeightedPauliOperator.sorted_grouping)

        # check all paulis are still existed.
        for g_p in grouped_op.paulis:
            passed = False
            for pauli in op.paulis:
                if pauli[1] == g_p[1]:
                    passed = pauli[0] == g_p[0]
                    break
            self.assertTrue(passed,
                            "non-existed paulis in grouped_paulis: {}".format(g_p[1].to_label()))

        # check the number of basis of grouped
        # one should be less than and equal to the original one.
        self.assertGreaterEqual(len(op.basis), len(grouped_op.basis))
    def test_chop(self):
        """ chop test """
        paulis = [Pauli.from_label(x) for x in ['IIXX', 'ZZXX', 'ZZZZ', 'XXZZ', 'XXXX', 'IXXX']]
        coeffs = [0.2, 0.3, 0.4, 0.5, 0.6, 0.7]
        op = WeightedPauliOperator.from_list(paulis, coeffs)
        grouped_op = op_converter.to_tpb_grouped_weighted_pauli_operator(
            op, TPBGroupedWeightedPauliOperator.sorted_grouping)

        original_num_basis = len(grouped_op.basis)
        chopped_grouped_op = grouped_op.chop(0.35, copy=True)
        self.assertLessEqual(len(chopped_grouped_op.basis), 3)
        self.assertLessEqual(len(chopped_grouped_op.basis), original_num_basis)
        # ZZXX group is remove
        for b, _ in chopped_grouped_op.basis:
            self.assertFalse(b.to_label() == 'ZZXX')

        chopped_grouped_op = grouped_op.chop(0.55, copy=True)
        self.assertLessEqual(len(chopped_grouped_op.basis), 1)
        self.assertLessEqual(len(chopped_grouped_op.basis), original_num_basis)

        for b, _ in chopped_grouped_op.basis:
            self.assertFalse(b.to_label() == 'ZZXX')
            self.assertFalse(b.to_label() == 'ZZZZ')
            self.assertFalse(b.to_label() == 'XXZZ')
Пример #11
0
    def test_evaluate_single_pauli_statevector(self):
        """ evaluate single pauli statevector test """
        # X
        op = WeightedPauliOperator.from_list([Pauli('X')])
        qr = QuantumRegister(1, name='q')
        wave_function = QuantumCircuit(qr)
        # + 1 eigenstate
        wave_function.h(qr[0])
        circuits = op.construct_evaluation_circuit(wave_function=wave_function,
                                                   statevector_mode=True)
        result = self.quantum_instance_statevector.execute(circuits)
        actual_value = op.evaluate_with_result(result=result,
                                               statevector_mode=True)
        self.assertAlmostEqual(1.0, actual_value[0].real, places=5)
        # - 1 eigenstate
        wave_function = QuantumCircuit(qr)
        wave_function.x(qr[0])
        wave_function.h(qr[0])
        circuits = op.construct_evaluation_circuit(wave_function=wave_function,
                                                   statevector_mode=True)
        result = self.quantum_instance_statevector.execute(circuits)
        actual_value = op.evaluate_with_result(result=result,
                                               statevector_mode=True)
        self.assertAlmostEqual(-1.0, actual_value[0].real, places=5)

        # Y
        op = WeightedPauliOperator.from_list([Pauli('Y')])
        qr = QuantumRegister(1, name='q')
        wave_function = QuantumCircuit(qr)
        # + 1 eigenstate
        wave_function.h(qr[0])
        wave_function.s(qr[0])
        circuits = op.construct_evaluation_circuit(wave_function=wave_function,
                                                   statevector_mode=True)
        result = self.quantum_instance_statevector.execute(circuits)
        actual_value = op.evaluate_with_result(result=result,
                                               statevector_mode=True)
        self.assertAlmostEqual(1.0, actual_value[0].real, places=5)
        # - 1 eigenstate
        wave_function = QuantumCircuit(qr)
        wave_function.x(qr[0])
        wave_function.h(qr[0])
        wave_function.s(qr[0])
        circuits = op.construct_evaluation_circuit(wave_function=wave_function,
                                                   statevector_mode=True)
        result = self.quantum_instance_statevector.execute(circuits)
        actual_value = op.evaluate_with_result(result=result,
                                               statevector_mode=True)
        self.assertAlmostEqual(-1.0, actual_value[0].real, places=5)

        # Z
        op = WeightedPauliOperator.from_list([Pauli('Z')])
        qr = QuantumRegister(1, name='q')
        wave_function = QuantumCircuit(qr)
        # + 1 eigenstate
        circuits = op.construct_evaluation_circuit(wave_function=wave_function,
                                                   statevector_mode=True)
        result = self.quantum_instance_statevector.execute(circuits)
        actual_value = op.evaluate_with_result(result=result,
                                               statevector_mode=True)
        self.assertAlmostEqual(1.0, actual_value[0].real, places=5)
        # - 1 eigenstate
        wave_function = QuantumCircuit(qr)
        wave_function.x(qr[0])
        circuits = op.construct_evaluation_circuit(wave_function=wave_function,
                                                   statevector_mode=True)
        result = self.quantum_instance_statevector.execute(circuits)
        actual_value = op.evaluate_with_result(result=result,
                                               statevector_mode=True)
        self.assertAlmostEqual(-1.0, actual_value[0].real, places=5)