コード例 #1
0
    def test_dia_matrix(self):
        """
            test conversion to dia_matrix
        """
        num_qubits = 4
        pauli_term = []
        for pauli_label in itertools.product('IZ', repeat=num_qubits):
            coeff = np.random.random(1)[0]
            pauli_term.append([coeff, Pauli.from_label(pauli_label)])
        op = Operator(paulis=pauli_term)

        op.to_matrix()
        op.to_grouped_paulis()
        op._to_dia_matrix('grouped_paulis')

        self.assertEqual(op.matrix.ndim, 1)

        num_qubits = 4
        pauli_term = []
        for pauli_label in itertools.product('YZ', repeat=num_qubits):
            coeff = np.random.random(1)[0]
            pauli_term.append([coeff, Pauli.from_label(pauli_label)])
        op = Operator(paulis=pauli_term)

        op.to_matrix()
        op._to_dia_matrix('matrix')

        self.assertEqual(op.matrix.ndim, 2)
コード例 #2
0
    def test_subtraction_matrix(self):
        """
            test subtraction in the matrix mode
        """
        pauli_a = 'IX'
        pauli_b = 'ZY'
        coeff_a = 0.5
        coeff_b = 0.5
        pauli_term_a = [coeff_a, Pauli.from_label(pauli_a)]
        pauli_term_b = [coeff_b, Pauli.from_label(pauli_b)]
        op_a = Operator(paulis=[pauli_term_a])
        op_b = Operator(paulis=[pauli_term_b])
        op_a.to_matrix()
        op_b.to_matrix()
        op_a -= op_b
        op_a.to_paulis()
        self.assertEqual(2, len(op_a.paulis))
        self.assertEqual(0.5, op_a.paulis[0][0])
        self.assertEqual(-0.5, op_a.paulis[1][0])

        pauli_c = 'IX'
        coeff_c = 0.25
        pauli_term_c = [coeff_c, Pauli.from_label(pauli_c)]
        op_c = Operator(paulis=[pauli_term_c])
        op_c.to_matrix()
        op_a.to_matrix()
        op_a -= op_c

        op_a.to_paulis()
        self.assertEqual(2, len(op_a.paulis))
        self.assertEqual(0.25, op_a.paulis[0][0])