Skip to content

Commit

Permalink
trivial changes for more idiomatic python (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinsung authored Sep 17, 2024
1 parent 91e046e commit a2366bd
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 14 deletions.
6 changes: 3 additions & 3 deletions qiskit_addon_sqd/counts.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def counts_to_arrays(counts: dict[str, float | int]) -> tuple[np.ndarray, np.nda
bit's value
- A 1D array containing the probability with which each bitstring was sampled
"""
if counts == {}:
if not counts:
return np.array([]), np.array([])
prob_dict = normalize_counts_dict(counts)
bs_mat = np.array([[bit == "1" for bit in bitstring] for bitstring in prob_dict])
Expand Down Expand Up @@ -151,9 +151,9 @@ def generate_counts_bipartite_hamming(

def normalize_counts_dict(counts: dict[str, float | int]) -> dict[str, float]:
"""Convert a counts dictionary into a probability dictionary."""
if counts == {}:
if not counts:
return counts

total_counts = float(sum(counts.values()))
total_counts = sum(counts.values())

return {bs: count / total_counts for bs, count in counts.items()}
16 changes: 8 additions & 8 deletions qiskit_addon_sqd/fermion.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ def solve_fermion(
"""
addresses = _check_addresses(addresses)

num_up = bin(addresses[0][0])[2:].count("1")
num_dn = bin(addresses[1][0])[2:].count("1")
num_up = format(addresses[0][0], "b").count("1")
num_dn = format(addresses[1][0], "b").count("1")

# Number of molecular orbitals
norb = hcore.shape[0]
Expand Down Expand Up @@ -163,8 +163,8 @@ def optimize_orbitals(
"""
addresses = _check_addresses(addresses)

num_up = bin(addresses[0][0])[2:].count("1")
num_dn = bin(addresses[1][0])[2:].count("1")
num_up = format(addresses[0][0], "b").count("1")
num_dn = format(addresses[1][0], "b").count("1")

# TODO: Need metadata showing the optimization history
## hcore and eri in physicist ordering
Expand Down Expand Up @@ -339,17 +339,17 @@ def _check_addresses(
) -> tuple[np.ndarray, np.ndarray]:
"""Make sure the hamming weight is consistent in all determinants."""
addr_up, addr_dn = addresses
addr_up_ham = bin(addr_up[0])[2:].count("1")
addr_up_ham = format(addr_up[0], "b").count("1")
for i, addr in enumerate(addr_up):
ham = bin(addr)[2:].count("1")
ham = format(addr, "b").count("1")
if ham != addr_up_ham:
raise ValueError(
f"Spin-up address in index 0 has hamming weight {addr_up_ham}, but address in "
f"index {i} has hamming weight {ham}."
)
addr_dn_ham = bin(addr_dn[0])[2:].count("1")
addr_dn_ham = format(addr_dn[0], "b").count("1")
for i, addr in enumerate(addr_dn):
ham = bin(addr)[2:].count("1")
ham = format(addr, "b").count("1")
if ham != addr_dn_ham:
raise ValueError(
f"Spin-down address in index 0 has hamming weight {addr_dn_ham}, but address in "
Expand Down
5 changes: 3 additions & 2 deletions qiskit_addon_sqd/subsampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,10 @@ def postselect_and_subsample(
ValueError: Hamming weights must be non-negative integers.
ValueError: Samples per batch and number of batches must be positive integers.
"""
if bitstring_matrix.shape[0] < 1:
num_bitstrings = len(bitstring_matrix)
if num_bitstrings == 0:
return [np.array([])] * num_batches
if len(probabilities) != bitstring_matrix.shape[0]:
if len(probabilities) != num_bitstrings:
raise ValueError(
"The number of elements in the probabilities array must match the number of rows in the bitstring matrix."
)
Expand Down
2 changes: 1 addition & 1 deletion test/test_counts.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
class TestCounts(unittest.TestCase):
def setUp(self):
self.max_val = 16
self.counts = {bin(i)[2:].zfill(4): 100 for i in range(self.max_val)}
self.counts = {format(i, "04b"): 100 for i in range(self.max_val)}

def test_counts_to_arrays(self):
with self.subTest("Basic test"):
Expand Down

0 comments on commit a2366bd

Please sign in to comment.