Skip to content

Rework repetition code tutorial#4631

Open
jlapeyre wants to merge 9 commits intoQiskit:mainfrom
jlapeyre:repetition-code-refresh
Open

Rework repetition code tutorial#4631
jlapeyre wants to merge 9 commits intoQiskit:mainfrom
jlapeyre:repetition-code-refresh

Conversation

@jlapeyre
Copy link
Contributor

@jlapeyre jlapeyre commented Feb 5, 2026

Changes made in this PR

This commit makes significant changes to the organization of the tutorial on the 3-qubit repetition code, while leaving the structure and calculations nearly unchanged.

Notebook with changes proposed in this PR

Some of the changes are.

  • Removed discussion of stabilizers

  • Changed text introducing sections in various ways.

  • Registers are no longer global. Rather, are created in and returned from a function.

  • Variable names have been changed to follow somewhat common usage in text books.

  • Some calculation and reports are factored into functions.

  • Shot dicts are sorted to have the same bitstring order within the notebook as well as between runs.

  • A bit more explanation of reading error syndrome.

  • Transpilation discussion improved.

  • "isa_" names retained. But now, definition of ISA given.

  • Circuits with correction and without were run in separate sampler jobs. Now they are run in a single job.

  • Number of shots 10_000 rather than 1000

  • Put backend.least_busy(... in "Setup".

  • Removed code that resets the ancilla qubits after the error syndrome is read into the classical register. This would be relevant if we were doing further work in the circuit and wanted to do additional rounds of error correction. (See issue below)

  • The existing code labels the first of the code qubits, i.e. the data qubits, as the one that carries the input state: state_data = qreg_data[0]. I like this idea and tried to retain it. But in the end, it made the functions a bit more complicated, and less readable. So I dropped this idea.

Issues

There are still some issues that could be addressed. But this PR was taking a lot of time, so I have not addressed them

Write a summary or conclusion or observation

As it stands, using error correction makes the results worse, fairly consistently. This is not what we want. I went over the circuit a few times. I haven't yet found a problem. I also have run the code with the main branch (before this commit) and I find the same problem. I mostly did this with a fake backend in order to collect better statistics.

I suspect something like a coding error such that a qubit that is correct is nonetheless flipped.

Mid-circuit measurements

  • measure_2 (MidCircuitMeasure) is not supported on AerSimulator. A user who plays with this notebook might want to use a fake backend, or another backend that does not support measure_2. But we have hard-coded use of this instruction. A fake backend carries exactly the same instruction set as the backend that it mimics. In particular, it will "support" measure_2. But running the Sampler raises an error because AerSimulator does not support it.

One solution is

def is_simulator(backend):
    is_sim = getattr(backend, "simulator", None)
    if is_sim is None:
        is_sim = backend.configuration().simulator
    return is_sim

def has_measure_2(backend):
    if is_simulator(backend):
        return False
    return any(b[0].name == "measure_2" for b in backend.instructions)

have_measure_2 = has_measure_2(backend)

and

    if have_measure_2:
        for bit_ind in (0, 1):
            circuit.append(MidCircuitMeasure(),
                           [ancilla_qubits[bit_ind]], [syndrome_clbits[bit_ind]])

    else:
        circuit.measure(ancilla_qubits, syndrome_clbits)
  • Does MidCircuitMeasure() support more than one qubit? The doc string says, 1 qubt, 1 clbit. However, it is a subclass of Instruction, so that the docstring for the attributes are appended and these say you can specify num_qubits and num_clbits. I (or someone) could just try this. Using MidCircuitMeasure is a bit more complicated than the method measure.

  • When using MidCircuitMeasurement, the plot of the circuit is a bit less clear than it was using measure(). In the case at hand, there are two syndrome measurements. Using measure() you can see each line. But MidCircuitMeasurement draws a box with no structure around the two. The latter is probably better for a large number of wires.

Explanation of statistics

We could explain why we count both 000 and 111 as success when the real correct result is 111.

Resetting ancillas

As mentioned above, I removed code that resets the ancillas after reading the syndrome. In this experiment, there is no point in resetting them. However, this code could be presented and discussed in a section after the main experiment.

This commit makes signficicant changes to the organization of the tutorial on
the 3-qubit repetition code, while leaving the structure and calculations nearly
unchanged.

Some of the changes re.

* Removed discussion of stabilizers

* Changed text introducing sections in various ways.

* Registers are no longer global. Rather are created in and returned from a
  function.

* Variable names have been changed to follow somewhat common usage in text
  books.

* Some calculation and reports are factored into functions.

* Shot dicts are sorted to have the same bitstring order within the notebook as
  well as between runs.

* A bit more explanation of reading error syndrome.

* Transpilation discussion improved.

* "isa_" names retained. But now, definiition of ISA given.

* Circuits with correction and without were run in separate sampler jobs. Now
  they are run in a single job.
@review-notebook-app
Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@qiskit-bot
Copy link
Contributor

One or more of the following people are relevant to this code:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

2 participants