def test_trotterization(self): circ_vec = [Circuit(), build_circuit('Z_0')] coef_vec = [-1.0j * 0.5, -1.0j * -0.04544288414432624] # the operator to be exponentiated minus_iH = QubitOperator() for i in range(len(circ_vec)): minus_iH.add(coef_vec[i], circ_vec[i]) # exponentiate the operator Utrot, phase = trotterization.trotterize(minus_iH) inital_state = np.zeros(2**4, dtype=complex) inital_state[3] = np.sqrt(2 / 3) inital_state[12] = -np.sqrt(1 / 3) # initalize a quantum computer with above coeficients # i.e. ca|1100> + cb|0011> qc = Computer(4) qc.set_coeff_vec(inital_state) # apply the troterized minus_iH qc.apply_circuit(Utrot) qc.apply_constant(phase) smart_print(qc) coeffs = qc.get_coeff_vec() assert np.real(coeffs[3]) == approx(0.6980209737879599, abs=1.0e-15) assert np.imag(coeffs[3]) == approx(-0.423595782342996, abs=1.0e-15) assert np.real(coeffs[12]) == approx(-0.5187235657531178, abs=1.0e-15) assert np.imag(coeffs[12]) == approx(0.25349397560041553, abs=1.0e-15)
def test_sparse_operator(self): """ test the SparseMatrix and SparseVector classes """ coeff_vec = [ -0.093750, +0.093750j, -0.093750, -0.093750j, -0.093750, -0.093750j, +0.062500j, -0.093750, -0.093750, +0.093750j, +0.093750, -0.062500, -0.093750j, -0.062500j, +0.062500j, -0.062500, +0.062500, +0.062500, -0.062500j, -0.093750, +0.062500j, -0.062500, -0.062500j, -0.062500, +0.093750j, +0.093750j, +0.062500j, +0.093750, -0.062500, -0.062500, -0.093750j, -0.062500j ] circ_vec = [ build_circuit('X_1 Y_2 X_3 Y_4'), build_circuit('X_1 Y_2 X_3 X_4'), build_circuit('X_1 Y_2 Y_3 X_4'), build_circuit('X_1 X_2 X_3 Y_4'), build_circuit('X_1 X_2 X_3 X_4'), build_circuit('X_1 X_2 Y_3 X_4'), build_circuit('Y_2 X_3 X_4 Z_5 X_6'), build_circuit('Y_1 Y_2 Y_3 Y_4'), build_circuit('Y_1 X_2 X_3 Y_4'), build_circuit('Y_1 X_2 X_3 X_4'), build_circuit('Y_1 Y_2 X_3 X_4'), build_circuit('X_2 X_3 X_4 Z_5 X_6'), build_circuit('Y_1 X_2 Y_3 Y_4'), build_circuit('X_2 Y_3 Y_4 Z_5 Y_6'), build_circuit('X_2 Y_3 X_4 Z_5 X_6'), build_circuit('X_2 Y_3 Y_4 Z_5 X_6'), build_circuit('X_2 X_3 Y_4 Z_5 Y_6'), build_circuit('Y_2 Y_3 X_4 Z_5 X_6'), build_circuit('X_2 X_3 Y_4 Z_5 X_6'), build_circuit('Y_1 X_2 Y_3 X_4'), build_circuit('Y_2 Y_3 X_4 Z_5 Y_6'), build_circuit('Y_2 X_3 X_4 Z_5 Y_6'), build_circuit('X_2 X_3 X_4 Z_5 Y_6'), build_circuit('X_2 Y_3 X_4 Z_5 Y_6'), build_circuit('Y_1 Y_2 Y_3 X_4'), build_circuit('Y_1 Y_2 X_3 Y_4'), build_circuit('Y_2 Y_3 Y_4 Z_5 X_6'), build_circuit('X_1 X_2 Y_3 Y_4'), build_circuit('Y_2 Y_3 Y_4 Z_5 Y_6'), build_circuit('Y_2 X_3 Y_4 Z_5 X_6'), build_circuit('X_1 Y_2 Y_3 Y_4'), build_circuit('Y_2 X_3 Y_4 Z_5 Y_6') ] qubit_op = QubitOperator() for coeff, circ in zip(coeff_vec, circ_vec): qubit_op.add(coeff, circ) num_qb = qubit_op.num_qubits() qci = Computer(num_qb) arb_vec = np.linspace(0, 2 * np.pi, 2**num_qb) arb_vec = arb_vec / np.linalg.norm(arb_vec) qci.set_coeff_vec(arb_vec) sp_mat_op = qubit_op.sparse_matrix(num_qb) ci = np.array(qci.get_coeff_vec()) qci.apply_operator(qubit_op) diff_vec = np.array(qci.get_coeff_vec()) # Reset stae qci.set_coeff_vec(ci) qci.apply_sparse_matrix(sp_mat_op) # see if there is a difference diff_vec = np.array(qci.get_coeff_vec()) - diff_vec diff_val = np.linalg.norm(diff_vec) print('Operator used for sparse matrix operator test: \n', qubit_op) print('||∆||: ', diff_val) assert diff_val < 1.0e-15