-
Notifications
You must be signed in to change notification settings - Fork 3
Implement Quantum Annealing Ising Hamiltonian simulator #25
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
Open
SimoneGasperini
wants to merge
6
commits into
main
Choose a base branch
from
ising-simulation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5a751aa
Add first naive implementation of the Ising Hamiltonian simulator
SimoneGasperini f12decf
Fix sign of the transverse field in the Hamiltonian
SimoneGasperini d0e157b
Move to scipy.sparse representation of the final Hamiltonian diagonal…
SimoneGasperini 5da0eb6
Move to scipy.sparse CSR representation of the Hamiltonian matrix
SimoneGasperini 8d5a344
Minor fix for scipy.sparse representation format
SimoneGasperini 2611e9d
Add energy offset to the Ising Hamiltonian model
SimoneGasperini File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import functools | ||
| import numpy as np | ||
|
|
||
|
|
||
| identity = np.array([[1, 0], [0, 1]]) | ||
| sigma_x = np.array([[0, 1], [1, 0]]) | ||
| sigma_z = np.array([[1, 0], [0, -1]]) | ||
|
|
||
|
|
||
| class IsingHamiltonianSimulator: | ||
| def __init__(self, anneal_schedule=None, time_steps=100): | ||
| self.anneal_schedule = self.default_anneal_schedule if anneal_schedule is None else anneal_schedule | ||
| self.time_steps = time_steps | ||
|
|
||
| @staticmethod | ||
| def default_anneal_schedule(s): | ||
| a = 1 - s | ||
| b = s | ||
| return a, b | ||
|
|
||
| @staticmethod | ||
| def get_H_init(num_qubits): | ||
| H_init = np.zeros(shape=(2**num_qubits, 2**num_qubits)) | ||
| for i in range(num_qubits): | ||
| terms = [identity] * num_qubits | ||
| terms[i] = sigma_x | ||
| H_init -= functools.reduce(np.kron, terms) | ||
| return H_init | ||
|
|
||
| @staticmethod | ||
| def get_H_final(num_qubits, h, J): | ||
| H_final = np.zeros(shape=(2**num_qubits, 2**num_qubits)) | ||
| for i in range(num_qubits): | ||
| terms = [identity] * num_qubits | ||
| terms[i] = sigma_z | ||
| H_final += h[i] * functools.reduce(np.kron, terms) | ||
| for i in range(num_qubits): | ||
| for j in range(i + 1, num_qubits): | ||
| terms = [identity] * num_qubits | ||
| terms[i] = sigma_z | ||
| terms[j] = sigma_z | ||
| H_final += J[i, j] * functools.reduce(np.kron, terms) | ||
| return H_final | ||
|
|
||
| def run(self, bqm): | ||
| num_qubits = bqm.num_variables | ||
| linear, quadratic, _ = bqm.to_ising() | ||
| h = np.array([linear.get(i, 0) for i in range(num_qubits)]) | ||
| J = np.array([[quadratic.get((i, j), 0) for i in range(num_qubits)] for j in range(num_qubits)]) | ||
| H_init = self.get_H_init(num_qubits=num_qubits) | ||
| H_final = self.get_H_final(num_qubits=num_qubits, h=h, J=J) | ||
| time = np.linspace(start=0, stop=1, num=self.time_steps) | ||
| for s in time: | ||
| a, b = self.anneal_schedule(s=s) | ||
| H_ising = (a / 2) * H_init + (b / 2) * H_final | ||
| # What should we return here? | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.