Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify models.encodings.hamming_weight_encoder to upload data in the lexicographical order #1572

Merged
merged 4 commits into from
Feb 3, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/qibo/models/encodings.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,15 @@ def hamming_weight_encoder(
initial_string = np.array([1] * weight + [0] * (nqubits - weight))
bitstrings, targets_and_controls = _ehrlich_algorithm(initial_string)

# sort data such that the encoding is performed in lexicographical order
bitstrings_lexicographical = list(zip(bitstrings, range(len(bitstrings))))
bitstrings_lexicographical.sort()
bitstrings_lexicographical = np.asarray(bitstrings_lexicographical)
lexicographical_order = list(bitstrings_lexicographical[:, 1].astype(int))
bitstrings_lexicographical = list(bitstrings_lexicographical[:, 0])
data = data[lexicographical_order]
del bitstrings_lexicographical

# Calculate all gate phases necessary to encode the amplitudes.
_data = np.abs(data) if complex_data else data
thetas = _generate_rbs_angles(_data, nqubits, architecture="diagonal")
Expand Down
Loading