Tfim simulation using 2 CNOT rather than 3 CNOT#94
Open
Conversation
marekgluza
reviewed
Nov 5, 2024
marekgluza
reviewed
Nov 5, 2024
| steps: int = None | ||
| B_a: float = None | ||
|
|
||
| def circuit(self, a, t_duration, B_a, steps=None, order=None): |
Contributor
There was a problem hiding this comment.
Suggested change
| def circuit(self, a, t_duration, B_a, steps=None, order=None): | |
| def circuit(self, t_duration, steps=None, order=None): |
This class must implement eo.circuit(0.1) without requiring other parameters.
What is a?
Contributor
Author
There was a problem hiding this comment.
I use the notation in the paper where a is an index and B_a is another parameter.
marekgluza
reviewed
Nov 5, 2024
|
|
||
| for _ in range(steps): | ||
| # Apply time evolution for X(a) + B_a * Z(a) | ||
| circuit += self._time_evolution_step(a, dt, B_a) |
Contributor
There was a problem hiding this comment.
This is commuting so it's equivalent to
circuit += self._time_evolution_step(a, t_duration, B_a)
Once you have for a in range(self.nqubits): (TFIM evolution oracle should be implemented for nqubits) then you need to do the CNOTs before every dt step here
marekgluza
requested changes
Nov 5, 2024
|
|
||
| # Time evolution under the transverse field Ising model Hamiltonian | ||
| # exp(-i t (X(a) + B_a * Z(a))) | ||
| dt = t_duration / steps # Divide the time duration for Trotterization if needed |
Contributor
There was a problem hiding this comment.
Suggested change
| dt = t_duration / steps # Divide the time duration for Trotterization if needed | |
| dt = t_duration / steps # Divide the time duration for Trotterization if needed | |
| for a in range(nqubits: | |
| circuit.add(gates.CNOT(a, a + 1)) | |
| circuit += self._time_evolution_step(a, dt, B_a) | |
| circuit.add(gates.CNOT(a, a + 1)) |
and then loop this over _ in range(steps)
Contributor
Author
There was a problem hiding this comment.
n_qubits = 3
h_coeff = 1
hamiltonian = SymbolicHamiltonian(nqubits=n_qubits)
oracle = TFIM_EvolutionOracle(h=hamiltonian, evolution_oracle_type="trotter", steps=1, B_a=0, order=2)
circuit = oracle.circuit(t_duration=1.0)
unitary = circuit.unitary()
from qibo import hamiltonians
from numpy.linalg import norm
def our_TFIM(nqubits, h: float = 0.0, dense: bool = True, backend=None):
def multikron(matrix_list):
"""Calculates Kronecker product of a list of matrices."""
return reduce(np.kron, matrix_list)
from qibo.backends import matrices
matrix = (
- multikron([matrices.X, matrices.X]) - h * multikron([matrices.Z, matrices.I])
)
terms = [hamiltonians.terms.HamiltonianTerm(matrix, i, i + 1) for i in range(nqubits - 1)]
terms.append(hamiltonians.terms.HamiltonianTerm(matrix, nqubits - 1, 0))
ham = SymbolicHamiltonian(backend=backend)
ham.terms = terms
return ham
ham = our_TFIM(nqubits=n_qubits, h=h_coeff, dense=False)
truth = ham.exp(1)
verification_norm = []
for step in range(1, 21):
oracle = TFIM_EvolutionOracle(h=hamiltonian, evolution_oracle_type="trotter", steps=step, B_a=h_coeff, order=2)
circuit = oracle.circuit(t_duration=1.0)
unitary = circuit.unitary()
#print(norm(truth-unitary))
verification_norm.append(norm(truth-unitary))
import matplotlib.pyplot as plt
x = np.array([i for i in range(1, 21)])
plt.plot(x, verification_norm, 'o')
plt.title("verification of TFIM 2 CNOT implementation")
plt.xlabel("steps")
plt.ylabel("norm of difference")
plt.show()
produces the following graph:
Co-authored-by: Marek Gluza <marekgluza@gmail.com>
…se of commutivity.
…ed on the looping at the boundary condition for TFIM model.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This pull request is to address issue 70 where we consider the special TFIM model:
where we can express the time evolution as