def test_to_matrix(self): """to matrix text """ np.testing.assert_array_equal(X.to_matrix(), Operator.from_label('X').data) np.testing.assert_array_equal(Y.to_matrix(), Operator.from_label('Y').data) np.testing.assert_array_equal(Z.to_matrix(), Operator.from_label('Z').data) op1 = Y + H np.testing.assert_array_almost_equal(op1.to_matrix(), Y.to_matrix() + H.to_matrix()) op2 = op1 * .5 np.testing.assert_array_almost_equal(op2.to_matrix(), op1.to_matrix() * .5) op3 = (4 - .6j) * op2 np.testing.assert_array_almost_equal(op3.to_matrix(), op2.to_matrix() * (4 - .6j)) op4 = op3.tensor(X) np.testing.assert_array_almost_equal( op4.to_matrix(), np.kron(op3.to_matrix(), X.to_matrix())) op5 = op4.compose(H ^ I) np.testing.assert_array_almost_equal( op5.to_matrix(), np.dot(op4.to_matrix(), (H ^ I).to_matrix())) op6 = op5 + PrimitiveOp(Operator.from_label('+r').data) np.testing.assert_array_almost_equal( op6.to_matrix(), op5.to_matrix() + Operator.from_label('+r').data)
def test_io_consistency(self): """ consistency test """ new_op = X ^ Y ^ I label = 'XYI' # label = new_op.primitive.to_label() self.assertEqual(str(new_op.primitive), label) np.testing.assert_array_almost_equal(new_op.primitive.to_matrix(), Operator.from_label(label).data) self.assertEqual(new_op.primitive, Pauli(label=label)) x_mat = X.primitive.to_matrix() y_mat = Y.primitive.to_matrix() i_mat = np.eye(2, 2) np.testing.assert_array_almost_equal(new_op.primitive.to_matrix(), np.kron(np.kron(x_mat, y_mat), i_mat)) hi = np.kron(H.to_matrix(), I.to_matrix()) hi2 = Operator.from_label('HI').data hi3 = (H ^ I).to_matrix() np.testing.assert_array_almost_equal(hi, hi2) np.testing.assert_array_almost_equal(hi2, hi3) xy = np.kron(X.to_matrix(), Y.to_matrix()) xy2 = Operator.from_label('XY').data xy3 = (X ^ Y).to_matrix() np.testing.assert_array_almost_equal(xy, xy2) np.testing.assert_array_almost_equal(xy2, xy3) # Check if numpy array instantiation is the same as from Operator matrix_op = Operator.from_label('+r') np.testing.assert_array_almost_equal(PrimitiveOp(matrix_op).to_matrix(), PrimitiveOp(matrix_op.data).to_matrix()) # Ditto list of lists np.testing.assert_array_almost_equal(PrimitiveOp(matrix_op.data.tolist()).to_matrix(), PrimitiveOp(matrix_op.data).to_matrix())
def test_to_matrix(self): """to matrix text """ np.testing.assert_array_equal(X.to_matrix(), Operator.from_label('X').data) np.testing.assert_array_equal(Y.to_matrix(), Operator.from_label('Y').data) np.testing.assert_array_equal(Z.to_matrix(), Operator.from_label('Z').data) op1 = Y + H np.testing.assert_array_almost_equal(op1.to_matrix(), Y.to_matrix() + H.to_matrix()) op2 = op1 * .5 np.testing.assert_array_almost_equal(op2.to_matrix(), op1.to_matrix() * .5) op3 = (4 - .6j) * op2 np.testing.assert_array_almost_equal(op3.to_matrix(), op2.to_matrix() * (4 - .6j)) op4 = op3.tensor(X) np.testing.assert_array_almost_equal( op4.to_matrix(), np.kron(op3.to_matrix(), X.to_matrix())) op5 = op4.compose(H ^ I) np.testing.assert_array_almost_equal( op5.to_matrix(), np.dot(op4.to_matrix(), (H ^ I).to_matrix())) op6 = op5 + PrimitiveOp(Operator.from_label('+r').data) np.testing.assert_array_almost_equal( op6.to_matrix(), op5.to_matrix() + Operator.from_label('+r').data) param = Parameter("α") m = np.array([[0, -1j], [1j, 0]]) op7 = MatrixOp(m, param) np.testing.assert_array_equal(op7.to_matrix(), m * param) param = Parameter("β") op8 = PauliOp(primitive=Pauli(label="Y"), coeff=param) np.testing.assert_array_equal(op8.to_matrix(), m * param) param = Parameter("γ") qc = QuantumCircuit(1) qc.h(0) op9 = CircuitOp(qc, coeff=param) m = np.array([[1, 1], [1, -1]]) / np.sqrt(2) np.testing.assert_array_equal(op9.to_matrix(), m * param)
from docplex.mp.model import Model from qiskit.optimization.problems import QuadraticProgram from qiskit.optimization.converters import QuadraticProgramToIsing from qiskit import QuantumCircuit from qiskit.aqua.operators import StateFn from qiskit.aqua.operators import Z, X, Y, I, H, S import random count = 0 w = 0 P_0 = I.to_matrix() P_1 = X.to_matrix() P_2 = Y.to_matrix() P_3 = Z.to_matrix() HH = H.to_matrix() S = S.to_matrix() OPE = [P_0, HH, S] pauli = [P_0, P_1, P_2, P_3] Pauli = [I, X, Y, Z] R = 2 #number of iteration == R n = 5 #number of qubits x_matrix = np.zeros((R, n)) z_matrix = np.zeros((R, n)) matrix = np.zeros((R, 2*n)) x_vector = [] z_vector = [] vector = []