Skip to content

Commit

Permalink
Merge pull request #995 from qiboteam/randomize_rb
Browse files Browse the repository at this point in the history
Make RB random again
  • Loading branch information
hay-k authored Oct 17, 2024
2 parents 44ec734 + 4704913 commit 731c9b1
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 5 deletions.
19 changes: 18 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ pylint = "^2.17"
pytest = "^7.1.2"
pytest-cov = "^3.0.0"
pytest-env = "^0.8.1"
pytest-mock = "^3.14.0"

[tool.poetry.group.docs]
optional = true
Expand Down
8 changes: 4 additions & 4 deletions src/qibocal/protocols/randomized_benchmarking/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ def twoq_rb_acquisition(

def layer_circuit(
rb_gen: Callable, depth: int, target, interleave: str = None
) -> tuple[Circuit, dict]:
) -> tuple[Circuit, list]:
"""Creates a circuit of `depth` layers from a generator `layer_gen` yielding `Circuit` or `Gate`
and a dictionary with random indexes used to select the clifford gates.
Expand All @@ -601,14 +601,14 @@ def layer_circuit(
random_indexes = []
if isinstance(target, (str, int)):
nqubits = 1
rb_gen_layer = rb_gen.layer_gen_single_qubit()
rb_gen_layer = rb_gen.layer_gen_single_qubit
elif isinstance(target, Tuple): # Tuple for qubit pair
nqubits = 2
rb_gen_layer = rb_gen.layer_gen_two_qubit()
rb_gen_layer = rb_gen.layer_gen_two_qubit
# Build each layer, there will be depth many in the final circuit.
for _ in range(depth):
# Generate a layer.
new_layer, random_index = rb_gen_layer
new_layer, random_index = rb_gen_layer()
random_indexes.append(random_index)
new_circuit = Circuit(nqubits)
if nqubits == 1:
Expand Down
45 changes: 45 additions & 0 deletions tests/test_randomized_benchmarking.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from qibocal.protocols.randomized_benchmarking.utils import (
RB_Generator,
generate_inv_dict_cliffords_file,
layer_circuit,
load_cliffords,
random_clifford,
)
Expand Down Expand Up @@ -184,3 +185,47 @@ def test_generate_inv_dict_cliffords_file(tmp_path):
clifford_matrices_inv = load_inverse_cliffords(file_inv)

assert clifford_inv.files == clifford_matrices_inv.files


@pytest.mark.parametrize("depth", [1, 10, 34])
def test_layer_circuit_single_qubit(mocker, depth):
qubit = 0
rb_gen = RB_Generator(123)
single_qubit_spy = mocker.spy(rb_gen, "layer_gen_single_qubit")
two_qubit_spy = mocker.spy(rb_gen, "layer_gen_two_qubit")

circuit, indices = layer_circuit(rb_gen, depth, qubit)

# assert that generator was called expected number of times
assert single_qubit_spy.call_count == depth
assert two_qubit_spy.call_count == 0

# assert that results from generator calls were used
assert circuit.depth == depth
circuit_gates = {g for m in circuit.queue.moments for g in m}
indices = set(indices)
for gate, i in single_qubit_spy.spy_return_list:
assert gate in circuit_gates
assert i in indices


@pytest.mark.parametrize("depth", [2, 24, 47])
def test_layer_circuit_two_qubit(mocker, depth):
qubit_pair = (0, 1)
rb_gen = RB_Generator(123, file="2qubitCliffs.json")
single_qubit_spy = mocker.spy(rb_gen, "layer_gen_single_qubit")
two_qubit_spy = mocker.spy(rb_gen, "layer_gen_two_qubit")

circuit, indices = layer_circuit(rb_gen, depth, qubit_pair)

# assert that generator was called expected number of times
assert single_qubit_spy.call_count == 0
assert two_qubit_spy.call_count == depth

# assert that results from generator calls were used
assert circuit.depth >= depth
circuit_gates = [g for m in circuit.queue.moments for g in m if g is not None]
indices = set(indices)
for gates, i in two_qubit_spy.spy_return_list:
assert all(g in circuit_gates for g in gates)
assert i in indices

0 comments on commit 731c9b1

Please sign in to comment.