def get_excitation_list_qasm(excitation_list, var_parameters, gate_counter): qasm = [''] # iterate over all excitations (each excitation is represented by a sum of products of pauli operators) for i, excitation in enumerate(excitation_list): # print('Excitation ', i) # testing # iterate over the terms of each excitation (each term is a product of pauli operators, on different qubits) # TODO replace with the function from QasmUtils for exponent_term in excitation.terms: exponent_angle = var_parameters[i] * excitation.terms[exponent_term] assert exponent_angle.real == 0 exponent_angle = exponent_angle.imag qasm.append(QasmUtils.exponent_qasm(exponent_term, exponent_angle)) return ''.join(qasm)
def test_exponent_statevector(self): exp_operator = ((0, 'X'), (1, 'Z'), (2, 'Z')) qasm = QasmUtils.qasm_header(3) qasm += QasmUtils.exponent_qasm(exp_operator, -numpy.pi / 2) statevector = QiskitSimBackend.statevector_from_qasm(qasm) expected_statevector = numpy.zeros(8) expected_statevector[1] = 1 self.assertEqual(len(expected_statevector), len(statevector)) for i in range(len(statevector)): self.assertEqual(statevector[i].round(3), expected_statevector[i].round(3))
def test_exponent_statevectors(self): qubit_operators = [] # only symetric qubit operators will works, because qiskit and openfermion use different qubit orderings qubit_operators.append(openfermion.QubitOperator('Z0 Y1 Z2')) qubit_operators.append(openfermion.QubitOperator('X0 Y1 X2')) qubit_operators.append(openfermion.QubitOperator('Y0 X1 X2 Y3')) for qubit_operator in qubit_operators: qubit_operator_tuple = list(qubit_operator.terms.keys())[0] n_qubits = len(qubit_operator_tuple) for angle in range(10): angle = 2 * numpy.pi / 10 # <<< create a statevector using QiskitSimulation.get_exponent_qasm >>> qasm = QasmUtils.qasm_header(n_qubits) qasm += QasmUtils.exponent_qasm(qubit_operator_tuple, angle) qiskit_statevector = QiskitSimBackend.statevector_from_qasm( qasm) qiskit_statevector = qiskit_statevector * numpy.exp( 1j * angle) # correct for a global phase qiskit_statevector = qiskit_statevector.round( 2) # round for the purpose of testing # <<< create a statevector using MatrixCalculation.get_qubit_operator_exponent_matrix >>> exp_matrix = MatrixUtils.get_excitation_matrix( 1j * qubit_operator, n_qubits, angle).todense() # prepare initial statevector corresponding to state |0> array_statevector = numpy.zeros(2**n_qubits) array_statevector[0] = 1 # update statevector array_statevector = numpy.array( exp_matrix.dot(array_statevector))[0].round( 2) # round for the purpose of testing # <<<< compare both state vectors >>>> self.assertEqual(len(array_statevector), len(qiskit_statevector)) # check the components of the two vectors are equal for i in range(len(qiskit_statevector)): self.assertEqual(qiskit_statevector[i], array_statevector[i])