qermit.postselection¶
- class qermit.postselection.postselect_manager.PostselectMgr(compute_cbits, postselect_cbits)[source]¶
Class for tracking and applying post selection to results.
An example use case might be the following. Here a Bell state is prepared. We would like to keep one bit of the results, conditioned on the other being 0. That’s to say that the postselected bits should all be 0.
from pytket import Circuit, Bit, Qubit from pytket.circuit.display import render_circuit_jupyter # Two qubits. The result of measuring the first will # be used to postselect the result of measuring the second. post_q = Qubit(0) comp_q = Qubit(1) # Construct Bell state preparation circuit. circuit = Circuit() circuit.add_qubit(post_q) circuit.add_qubit(comp_q) circuit.H(post_q) circuit.CX(post_q, comp_q) post_b = Bit(0) comp_b = Bit(1) circuit.add_bit(post_b) circuit.add_bit(comp_b) circuit.Measure(comp_q, comp_b) circuit.Measure(post_q, post_b) render_circuit_jupyter(circuit)
Running this circuit gives a roughly equal mix of 00 and 11 computation basis states.
from qermit.postselection import PostselectMgr from pytket.extensions.quantinuum import QuantinuumBackend, QuantinuumAPIOffline backend = QuantinuumBackend( device_name="H1-1LE", api_handler = QuantinuumAPIOffline(), ) compiled_circuit = backend.get_compiled_circuit(circuit) result = backend.run_circuit(compiled_circuit, 100) result.get_counts()
Counter({(0, 0): 56, (1, 1): 44})
This class is straightforwardly initialised with the computation and post selection bits. The computation bits are post selected based on the results of the post selection bits.
postselect_mgr = PostselectMgr( compute_cbits=[comp_b], postselect_cbits=[post_b] )
- Parameters:
- Raises:
Exception – Raised if a bit is in both compute_cbits and postselect_cbits.
- merge_result(result)[source]¶
Transforms BackendResult so that postselection bits are removed, but no shots are removed by postselection.
merge_result = postselect_mgr.merge_result(result=result) merge_result.get_counts()
Counter({(0,): 56, (1,): 44})
- Parameters:
result (
BackendResult
) – Result to be transformed.- Return type:
- Returns:
Result with postselection bits removed.
- postselect_result(result)[source]¶
Transforms BackendResult to keep only shots which should be post selected.
post_result = postselect_mgr.postselect_result(result=result) post_result.get_counts()
Counter({(0,): 56})
- Parameters:
result (
BackendResult
) – Result to be modified.- Return type:
- Returns:
Postselected shots.
- qermit.postselection.postselect_mitres.gen_postselect_mitres(backend, postselect_mgr, **kwargs)[source]¶
Generates MitRes running given circuit and applying postselection.
In the following example we prepare and measure a Bell state.
from pytket import Circuit from pytket.circuit.display import render_circuit_jupyter circuit = Circuit(2,2).H(0).CX(0,1).measure_all() render_circuit_jupyter(circuit)
We would like to postselect one measurement outcome based on the other being 0. We prepare a postselect mitres accordingly.
from qermit.postselection import gen_postselect_mitres from pytket.extensions.quantinuum import QuantinuumBackend, QuantinuumAPIOffline from qermit.postselection import PostselectMgr backend = QuantinuumBackend( device_name="H1-1LE", api_handler = QuantinuumAPIOffline() ) postselect_mgr = PostselectMgr( compute_cbits=[circuit.bits[0]], postselect_cbits=[circuit.bits[1]] ) mitres = gen_postselect_mitres( backend=backend, postselect_mgr=postselect_mgr, ) mitres.get_task_graph()
We can then construct the experiment we wish to run, and pass it through the postselect mitres.
from qermit import CircuitShots circ_shots = CircuitShots( Circuit=backend.get_compiled_circuit(circuit), Shots=100 ) result_list = mitres.run([circ_shots]) result_list[0].get_counts()
Counter({(0,): 55})
- Parameters:
backend (
Backend
) – Backend on this circuits are run.postselect_mgr (
PostselectMgr
) – Postselection manager.
- Return type:
- Returns:
MitRes running given circuit and applying postselection.