Skip to content

Commit

Permalink
Merge branch 'master' into cuquantum_tests
Browse files Browse the repository at this point in the history
  • Loading branch information
renatomello committed Mar 6, 2024
2 parents 002e603 + d1fe5a9 commit 973b0f2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
9 changes: 7 additions & 2 deletions src/qibo/models/dbi/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
from hyperopt import hp, tpe

from qibo import symbols
from qibo.backends import _check_backend
from qibo.hamiltonians import SymbolicHamiltonian
from qibo.models.dbi.double_bracket import (
DoubleBracketGeneratorType,
DoubleBracketIteration,
)


def generate_Z_operators(nqubits: int):
def generate_Z_operators(nqubits: int, backend=None):
"""Generate a dictionary containing 1) all possible products of Pauli Z operators for L = n_qubits and 2) their respective names.
Return: Dictionary with operator names (str) as keys and operators (np.array) as values
Expand All @@ -36,6 +37,8 @@ def generate_Z_operators(nqubits: int):
dephasing_channel = (sum([Z_op @ h0 @ Z_op for Z_op in Z_ops])+h0)/2**nqubits
norm_diff = np.linalg.norm(delta_h0 - dephasing_channel)
"""

backend = _check_backend(backend)
# list of tuples, e.g. ('Z','I','Z')
combination_strings = product("ZI", repeat=nqubits)
output_dict = {}
Expand All @@ -46,7 +49,9 @@ def generate_Z_operators(nqubits: int):
op_name = "".join(zi_string_combination)
tensor_op = str_to_symbolic(op_name)
# append in output_dict
output_dict[op_name] = SymbolicHamiltonian(tensor_op).dense.matrix
output_dict[op_name] = SymbolicHamiltonian(
tensor_op, backend=backend
).dense.matrix
return output_dict


Expand Down
14 changes: 7 additions & 7 deletions tests/test_models_dbi.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
)
from qibo.quantum_info import random_hermitian

NSTEPS = 50
NSTEPS = 10
"""Number of steps for evolution."""


@pytest.mark.parametrize("nqubits", [3, 4, 5])
@pytest.mark.parametrize("nqubits", [1, 2])
def test_double_bracket_iteration_canonical(backend, nqubits):
h0 = random_hermitian(2**nqubits, backend=backend)
dbi = DoubleBracketIteration(
Expand All @@ -28,7 +28,7 @@ def test_double_bracket_iteration_canonical(backend, nqubits):
assert initial_off_diagonal_norm > dbi.off_diagonal_norm


@pytest.mark.parametrize("nqubits", [3, 4, 5])
@pytest.mark.parametrize("nqubits", [1, 2])
def test_double_bracket_iteration_group_commutator(backend, nqubits):
h0 = random_hermitian(2**nqubits, backend=backend)
d = backend.cast(np.diag(np.diag(backend.to_numpy(h0))))
Expand All @@ -47,7 +47,7 @@ def test_double_bracket_iteration_group_commutator(backend, nqubits):
assert initial_off_diagonal_norm > dbi.off_diagonal_norm


@pytest.mark.parametrize("nqubits", [3, 4, 5])
@pytest.mark.parametrize("nqubits", [1, 2])
def test_double_bracket_iteration_single_commutator(backend, nqubits):
h0 = random_hermitian(2**nqubits, backend=backend)
d = backend.cast(np.diag(np.diag(backend.to_numpy(h0))))
Expand All @@ -66,7 +66,7 @@ def test_double_bracket_iteration_single_commutator(backend, nqubits):
assert initial_off_diagonal_norm > dbi.off_diagonal_norm


@pytest.mark.parametrize("nqubits", [3, 4, 5])
@pytest.mark.parametrize("nqubits", [3, 4])
def test_hyperopt_step(backend, nqubits):
h0 = random_hermitian(2**nqubits, backend=backend)
d = backend.cast(np.diag(np.diag(backend.to_numpy(h0))))
Expand All @@ -77,7 +77,7 @@ def test_hyperopt_step(backend, nqubits):
delta = 0.02

step = dbi.hyperopt_step(
step_min=initial_step - delta, step_max=initial_step + delta, max_evals=100
step_min=initial_step - delta, step_max=initial_step + delta, max_evals=10
)

assert step != initial_step
Expand All @@ -92,7 +92,7 @@ def test_hyperopt_step(backend, nqubits):
step = dbi.hyperopt_step(
step_min=initial_step - delta,
step_max=initial_step + delta,
max_evals=100,
max_evals=10,
look_ahead=look_ahead,
)

Expand Down
16 changes: 9 additions & 7 deletions tests/test_models_dbi_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
"""Number of steps for evolution."""


@pytest.mark.parametrize("nqubits", [2, 3])
@pytest.mark.parametrize("nqubits", [1, 2])
def test_generate_Z_operators(backend, nqubits):
h0 = random_hermitian(2**nqubits)
dbi = DoubleBracketIteration(Hamiltonian(nqubits=nqubits, matrix=h0))
generate_Z = generate_Z_operators(nqubits)
h0 = random_hermitian(2**nqubits, backend=backend)
dbi = DoubleBracketIteration(
Hamiltonian(nqubits=nqubits, matrix=h0, backend=backend)
)
generate_Z = generate_Z_operators(nqubits, backend=backend)
Z_ops = list(generate_Z.values())

delta_h0 = dbi.diagonal_h_matrix
Expand All @@ -29,21 +31,21 @@ def test_generate_Z_operators(backend, nqubits):
assert norm_diff < 1e-3


@pytest.mark.parametrize("nqubits", [2, 3])
@pytest.mark.parametrize("nqubits", [1, 2])
@pytest.mark.parametrize("step", [0.1, None])
def test_select_best_dbr_generator(backend, nqubits, step):
h0 = random_hermitian(2**nqubits, seed=1, backend=backend)
dbi = DoubleBracketIteration(
Hamiltonian(nqubits, h0, backend=backend),
mode=DoubleBracketGeneratorType.single_commutator,
)
generate_Z = generate_Z_operators(nqubits)
generate_Z = generate_Z_operators(nqubits, backend=backend)
Z_ops = list(generate_Z.values())
initial_off_diagonal_norm = dbi.off_diagonal_norm

for _ in range(NSTEPS):
dbi, idx, step_optimize, flip = select_best_dbr_generator(
dbi, Z_ops, step=step, compare_canonical=True
dbi, Z_ops, step=step, compare_canonical=True, max_evals=5
)

assert initial_off_diagonal_norm > dbi.off_diagonal_norm

0 comments on commit 973b0f2

Please sign in to comment.