Skip to content

Commit

Permalink
Merge pull request #510 from SRI-International/compiler-opt-changes-h…
Browse files Browse the repository at this point in the history
…amsim

Compiler opt changes hamsim
  • Loading branch information
rtvuser1 authored Jun 21, 2024
2 parents 82de5e5 + c116b0b commit be8351c
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 40 deletions.
33 changes: 22 additions & 11 deletions hamlib/qiskit/benchmarks-qiskit.ipynb

Large diffs are not rendered by default.

30 changes: 27 additions & 3 deletions hamlib/qiskit/hamiltonian_simulation_kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from qiskit.quantum_info import SparsePauliOp, Pauli
from qiskit.circuit import QuantumCircuit
from qiskit.circuit.library import PauliEvolutionGate
from qiskit.quantum_info import SparsePauliOp



# Saved circuits and subcircuits for display
Expand All @@ -40,8 +40,7 @@
_use_XX_YY_ZZ_gates = False


from hamlib_utils import process_hamiltonian_file

from hamlib_utils import process_hamiltonian_file, needs_normalization, normalize_data_format, parse_hamiltonian_to_sparsepauliop, determine_qubit_count

def process_data(data):
"""
Expand All @@ -60,6 +59,31 @@ def process_data(data):
hamiltonian = sparse_pauliop(parsed_pauli_list, num_qubits)
return hamiltonian, num_qubits


def sparse_pauliop(terms, num_qubits):
"""
Construct a SparsePauliOp from a list of Pauli terms and the number of qubits.
Args:
terms (list): A list of tuples, where each tuple contains a dictionary representing the Pauli operators and
their corresponding qubit indices, and a complex coefficient.
num_qubits (int): The total number of qubits.
Returns:
SparsePauliOp: The Hamiltonian represented as a SparsePauliOp.
"""
pauli_list = []

for pauli_dict, coefficient in terms:
label = ['I'] * num_qubits # Start with identity on all qubits
for qubit, pauli_op in pauli_dict.items():
label[qubit] = pauli_op
label = ''.join(label)
pauli_list.append((label, coefficient))

hamiltonian = SparsePauliOp.from_list(pauli_list, num_qubits=num_qubits)
return hamiltonian

def create_circuit():
"""
Create a quantum circuit based on the Hamiltonian data from an HDF5 file.
Expand Down
26 changes: 0 additions & 26 deletions hamlib/qiskit/hamlib_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,32 +132,6 @@ def determine_qubit_count(terms):
max_qubit = max_in_term
return max_qubit + 1 # Since qubit indices start at 0

def sparse_pauliop(terms, num_qubits):
"""
Construct a SparsePauliOp from a list of Pauli terms and the number of qubits.
Args:
terms (list): A list of tuples, where each tuple contains a dictionary representing the Pauli operators and
their corresponding qubit indices, and a complex coefficient.
num_qubits (int): The total number of qubits.
Returns:
SparsePauliOp: The Hamiltonian represented as a SparsePauliOp.
"""
pauli_list = []

for pauli_dict, coefficient in terms:
label = ['I'] * num_qubits # Start with identity on all qubits
for qubit, pauli_op in pauli_dict.items():
label[qubit] = pauli_op
label = ''.join(label)
pauli_list.append((label, coefficient))

hamiltonian = SparsePauliOp.from_list(pauli_list, num_qubits=num_qubits)
return hamiltonian



def download_and_extract(filename, url):
"""
Download a file from a given URL and unzip it.
Expand Down

0 comments on commit be8351c

Please sign in to comment.