def test_bksf_mapping(self): """Test bksf mapping. The spectrum of bksf mapping should be half of jordan wigner mapping. """ driver = PySCFDriver(atom='H .0 .0 0.7414; H .0 .0 .0', unit=UnitsType.ANGSTROM, charge=0, spin=0, basis='sto3g') molecule = driver.run() fer_op = FermionicOperator(h1=molecule.one_body_integrals, h2=molecule.two_body_integrals) jw_op = fer_op.mapping('jordan_wigner') bksf_op = fer_op.mapping('bksf') jw_op = op_converter.to_matrix_operator(jw_op) bksf_op = op_converter.to_matrix_operator(bksf_op) jw_eigs = np.linalg.eigvals(jw_op.matrix.toarray()) bksf_eigs = np.linalg.eigvals(bksf_op.matrix.toarray()) jw_eigs = np.sort(np.around(jw_eigs.real, 6)) bksf_eigs = np.sort(np.around(bksf_eigs.real, 6)) overlapped_spectrum = np.sum(np.isin(jw_eigs, bksf_eigs)) self.assertEqual(overlapped_spectrum, jw_eigs.size // 2)
def test_to_weighted_pauli_operator(self): """ to weighted pauli operator """ mat_op = op_converter.to_matrix_operator(self.pauli_op) pauli_op = op_converter.to_weighted_pauli_operator(mat_op) pauli_op.rounding(8) self.pauli_op.rounding(8) self.assertEqual(pauli_op, self.pauli_op)
def test_hf_value(self, mapping): """ hf value test """ try: driver = PySCFDriver(atom='Li .0 .0 .0; H .0 .0 1.6', unit=UnitsType.ANGSTROM, charge=0, spin=0, basis='sto3g') except QiskitChemistryError: self.skipTest('PYSCF driver does not appear to be installed') qmolecule = driver.run() core = Hamiltonian(transformation=TransformationType.FULL, qubit_mapping=mapping, two_qubit_reduction=False, freeze_core=False, orbital_reduction=[]) qubit_op, _ = core.run(qmolecule) qubit_op = op_converter.to_matrix_operator(qubit_op) hrfo = HartreeFock(core.molecule_info['num_orbitals'], core.molecule_info['num_particles'], mapping.value, False) qc = hrfo.construct_circuit('vector') hf_energy = qubit_op.evaluate_with_statevector( qc)[0].real + core._nuclear_repulsion_energy self.assertAlmostEqual(qmolecule.hf_energy, hf_energy, places=8)
def set_adj_matrix(self, adj_matrix): maxcut_op, self.maxcut_shift = max_cut.get_operator(adj_matrix) # print("maxcut_op: ", maxcut_op, ", maxcut_shift: ", maxcut_shift) maxcut_to_matrix = op_c.to_matrix_operator(maxcut_op) self.eigenvalues = maxcut_to_matrix.dia_matrix self.calc_expectation_value() self.draw_expectation_grid()
def test_evolve(self, expansion_mode, evo_time, num_time_slices): """ evolve test """ expansion_orders = [1, 2, 3, 4] if expansion_mode == 'suzuki' else [1] 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)) pauli_op = WeightedPauliOperator.from_list(paulis, weights) matrix_op = op_converter.to_matrix_operator(pauli_op) state_in = Custom(num_qubits, state='random') # get the exact state_out from raw matrix multiplication state_out_exact = matrix_op.evolve( state_in=state_in.construct_circuit('vector'), evo_time=evo_time, num_time_slices=0) # self.log.debug('exact:\n%s', state_out_exact) self.log.debug('Under %s expansion mode:', expansion_mode) for expansion_order in expansion_orders: # assure every time the operator from the original one if expansion_mode == 'suzuki': self.log.debug('With expansion order %s:', expansion_order) state_out_matrix = matrix_op.evolve( state_in=state_in.construct_circuit('vector'), evo_time=evo_time, num_time_slices=num_time_slices, expansion_mode=expansion_mode, expansion_order=expansion_order) quantum_registers = QuantumRegister(pauli_op.num_qubits, name='q') qc = QuantumCircuit(quantum_registers) qc += state_in.construct_circuit('circuit', quantum_registers) qc += pauli_op.copy().evolve( evo_time=evo_time, num_time_slices=num_time_slices, quantum_registers=quantum_registers, expansion_mode=expansion_mode, expansion_order=expansion_order, ) state_out_circuit = self.quantum_instance_statevector.execute( qc).get_statevector(qc) self.log.debug('The fidelity between exact and matrix: %s', state_fidelity(state_out_exact, state_out_matrix)) self.log.debug('The fidelity between exact and circuit: %s', state_fidelity(state_out_exact, state_out_circuit)) f_mc = state_fidelity(state_out_matrix, state_out_circuit) self.log.debug('The fidelity between matrix and circuit: %s', f_mc) self.assertAlmostEqual(f_mc, 1)
def test_to_matrix_operator(self): """ to matrix operator """ pauli_op = op_converter.to_weighted_pauli_operator(self.mat_op) mat_op = op_converter.to_matrix_operator(pauli_op) diff = float(np.sum(np.abs(self.mat_op.matrix - mat_op.matrix))) self.assertAlmostEqual(0, diff, places=8)