Skip to content

Commit

Permalink
Merge branch 'master' into quantum_info_docs
Browse files Browse the repository at this point in the history
  • Loading branch information
renatomello committed Dec 19, 2024
2 parents 005c23c + ce6dc20 commit 74de364
Show file tree
Hide file tree
Showing 17 changed files with 48 additions and 2,388 deletions.
17 changes: 0 additions & 17 deletions doc/source/api-reference/qibo.rst
Original file line number Diff line number Diff line change
Expand Up @@ -194,23 +194,6 @@ Iterative Quantum Amplitude Estimation (IQAE)
:member-order: bysource


Double Bracket Iteration algorithm for Diagonalization
""""""""""""""""""""""""""""""""""""""""""""""""""""""

The Double Bracket Flow (DBF) has been presented `here <https://arxiv.org/abs/2206.11772>`_
as a novel strategy for preparing eigenstates of a quantum system. We implement in
Qibo a discretized version of the algorithm, which executes sequential Double
Bracket Iterations.

.. autoclass:: qibo.models.dbi.double_bracket.DoubleBracketGeneratorType
:members:
:member-order: bysource

.. autoclass:: qibo.models.dbi.double_bracket.DoubleBracketIteration
:members:
:member-order: bysource


.. _timeevolution:

Time evolution
Expand Down
1 change: 0 additions & 1 deletion doc/source/code-examples/tutorials/dbi/README.md

This file was deleted.

This file was deleted.

58 changes: 0 additions & 58 deletions examples/dbi/README.md

This file was deleted.

782 changes: 0 additions & 782 deletions examples/dbi/dbi_tutorial_basic_intro.ipynb

This file was deleted.

53 changes: 23 additions & 30 deletions src/qibo/hamiltonians/hamiltonians.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,9 +555,7 @@ def calculate_dense(self):
def expectation(self, state, normalize=False):
return Hamiltonian.expectation(self, state, normalize)

def expectation_from_circuit(
self, circuit: "Circuit", qubit_map: dict = None, nshots: int = 1000
) -> float:
def expectation_from_circuit(self, circuit: "Circuit", nshots: int = 1000) -> float:
"""
Calculate the expectation value from a circuit.
This even works for observables not completely diagonal in the computational
Expand All @@ -572,70 +570,63 @@ def expectation_from_circuit(
Args:
circuit (Circuit): input circuit.
qubit_map (dict): qubit map, defaults to ``{0: 0, 1: 1, ..., n: n}``
nshots (int): number of shots, defaults to 1000.
Returns:
(float): the calculated expectation value.
"""
from qibo import Circuit, gates

if qubit_map is None:
qubit_map = list(range(self.nqubits))
from qibo import gates

rotated_circuits = []
coefficients = []
Z_observables = []
q_maps = []
qubit_maps = []
for term in self.terms:
# store coefficient
coefficients.append(term.coefficient)
# Only care about non-I terms
non_identity_factors = [
factor for factor in term.factors if factor.name[0] != "I"
]
# build diagonal observable
Z_observables.append(
SymbolicHamiltonian(
prod([Z(q) for q in term.target_qubits]),
prod(Z(factor.target_qubit) for factor in non_identity_factors),
nqubits=circuit.nqubits,
backend=self.backend,
)
)
# Get the qubits we want to measure for each term
qubit_map = sorted(factor.target_qubit for factor in non_identity_factors)
# prepare the measurement basis and append it to the circuit
measurements = [
gates.M(factor.target_qubit, basis=factor.gate.__class__)
for factor in set(term.factors)
for factor in non_identity_factors
]
circ_copy = circuit.copy(True)
circ_copy.add(measurements)
rotated_circuits.append(circ_copy)
# map the original qubits into 0, 1, 2, ...
q_maps.append(
dict(
zip(
[qubit_map[q] for q in term.target_qubits],
range(len(term.target_qubits)),
)
)
)
# for mapping the obtained sample frequencies to the original qubits
qubit_maps.append(qubit_map)
frequencies = [
result.frequencies()
for result in self.backend.execute_circuits(rotated_circuits, nshots=nshots)
]
return sum(
[
coeff * obs.expectation_from_samples(freq, qmap)
for coeff, freq, obs, qmap in zip(
coefficients, frequencies, Z_observables, q_maps
)
]
coeff * obs.expectation_from_samples(freq, qubit_map)
for coeff, freq, obs, qubit_map in zip(
coefficients, frequencies, Z_observables, qubit_maps
)
)

def expectation_from_samples(self, freq: dict, qubit_map: dict = None) -> float:
def expectation_from_samples(self, freq: dict, qubit_map: list = None) -> float:
"""
Calculate the expectation value from the samples.
The observable has to be diagonal in the computational basis.
Args:
freq (dict): input frequencies of the samples.
qubit_map (dict): qubit map.
qubit_map (list): qubit map.
Returns:
(float): the calculated expectation value.
Expand All @@ -657,11 +648,13 @@ def expectation_from_samples(self, freq: dict, qubit_map: dict = None) -> float:
)
expvals = []
for term in self.terms:
qubits = term.target_qubits
qubits = {
factor.target_qubit for factor in term.factors if factor.name[0] != "I"
}
expvals.extend(
[
term.coefficient.real
* (-1) ** [state[qubit_map[q]] for q in qubits].count("1")
* (-1) ** [state[qubit_map.index(q)] for q in qubits].count("1")
for state in keys
]
)
Expand Down
Empty file removed src/qibo/models/dbi/__init__.py
Empty file.
Loading

0 comments on commit 74de364

Please sign in to comment.