Skip to content

Commit

Permalink
Merge pull request #36 from Jij-Inc/hot_fix/no_quad_term
Browse files Browse the repository at this point in the history
fix the issues #35
  • Loading branch information
yuyamashiro authored Dec 14, 2023
2 parents 319b2ba + 2d91987 commit 8bcd73a
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 9 deletions.
20 changes: 11 additions & 9 deletions jijmodeling_transpiler_quantum/core/qrac/graph_coloring.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,20 @@ def check_linear_term(
for v in color_group.values():
idx_in_color_group.extend(v)

max_idx = max(color_group.keys())
value_counter = 1
# We're adding a bit to the next index of the 'quad' qubit, affecting only the linear term.
# If the Ising Hamiltonian has only linear terms, making 'quad' empty and causing a 'max' error, we return index 0 to avoid this.
qubit_index_for_linear = max(color_group.keys()) + 1 if color_group else 0
bits_in_qubits_counter = 1
for idx in linear_term_index:
if idx not in idx_in_color_group:
if value_counter == 1:
color_group[max_idx + 1] = []
if bits_in_qubits_counter == 1:
color_group[qubit_index_for_linear] = []

color_group[max_idx + 1].append(idx)
value_counter += 1
color_group[qubit_index_for_linear].append(idx)
bits_in_qubits_counter += 1

if value_counter > max_color_group_size:
max_idx += 1
value_counter = 1
if bits_in_qubits_counter > max_color_group_size:
qubit_index_for_linear += 1
bits_in_qubits_counter = 1

return color_group
46 changes: 46 additions & 0 deletions tests/test_qrac.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,49 @@ def test_check_linear_term_quri():
qrac_hamiltonian, offset, encoding = jtqp.qrao.qrac21_encode_ising(
ising, color_group
)


def test_check_no_quad_term_quri():
ising = IsingModel({}, {0:1.0,1:1.0,2: 5.0, 3: 2.0}, 6.0)
max_color_group_size = 3
_, color_group = greedy_graph_coloring(
ising.quad.keys(), max_color_group_size=max_color_group_size
)
color_group = check_linear_term(
color_group, ising.linear.keys(), max_color_group_size
)

qrac_hamiltonian, offset, encoding = jtqp.qrao.qrac31_encode_ising(
ising, color_group
)

ising = IsingModel({}, {2: 5.0, 3: 2.0}, 6.0)
max_color_group_size = 3
_, color_group = greedy_graph_coloring(
ising.quad.keys(), max_color_group_size=max_color_group_size
)
color_group = check_linear_term(
color_group, ising.linear.keys(), max_color_group_size
)

qrac_hamiltonian, offset, encoding = jtqp.qrao.qrac31_encode_ising(
ising, color_group
)

ising = IsingModel(
{},
{2: 5.0, 3: 2.0, 4: 1.0, 5: 1.0, 6: 1.0},
6.0,
)
max_color_group_size = 2
_, color_group = greedy_graph_coloring(
ising.quad.keys(), max_color_group_size=max_color_group_size
)
color_group = check_linear_term(
color_group, ising.linear.keys(), max_color_group_size
)

qrac_hamiltonian, offset, encoding = jtqp.qrao.qrac21_encode_ising(
ising, color_group
)

0 comments on commit 8bcd73a

Please sign in to comment.