1.2 Entanglement Entropy by Randomized MeasurementΒΆ


Basic UsageΒΆ

a. Import the instancesΒΆ

from qurry import EntropyMeasure

experiment_randomized = EntropyMeasure()
# It's default method. EntropyMeasure(method='randomized') also works

b. Preparing quantum circuitΒΆ

from qiskit import QuantumCircuit
from qurry.recipe import TrivialParamagnet, GHZ
sample01 = TrivialParamagnet(8)
print("| trivial paramagnet in 8 qubits:")
print(sample01)
| trivial paramagnet in 8 qubits:
     β”Œβ”€β”€β”€β”
q_0: ─ H β”œ
     β”œβ”€β”€β”€β”€
q_1: ─ H β”œ
     β”œβ”€β”€β”€β”€
q_2: ─ H β”œ
     β”œβ”€β”€β”€β”€
q_3: ─ H β”œ
     β”œβ”€β”€β”€β”€
q_4: ─ H β”œ
     β”œβ”€β”€β”€β”€
q_5: ─ H β”œ
     β”œβ”€β”€β”€β”€
q_6: ─ H β”œ
     β”œβ”€β”€β”€β”€
q_7: ─ H β”œ
     β””β”€β”€β”€β”˜
sample02 = GHZ(8)
print("| GHZ in 8 qubits:")
print(sample02)
| GHZ in 8 qubits:
     β”Œβ”€β”€β”€β”                                   
q_0: ─ H β”œβ”€β”€β– β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
     β””β”€β”€β”€β”˜β”Œβ”€β”΄β”€β”                              
q_1: ────── X β”œβ”€β”€β– β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
          β””β”€β”€β”€β”˜β”Œβ”€β”΄β”€β”                         
q_2: ─────────── X β”œβ”€β”€β– β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
               β””β”€β”€β”€β”˜β”Œβ”€β”΄β”€β”                    
q_3: ──────────────── X β”œβ”€β”€β– β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
                    β””β”€β”€β”€β”˜β”Œβ”€β”΄β”€β”               
q_4: ───────────────────── X β”œβ”€β”€β– β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
                         β””β”€β”€β”€β”˜β”Œβ”€β”΄β”€β”          
q_5: ────────────────────────── X β”œβ”€β”€β– β”€β”€β”€β”€β”€β”€β”€
                              β””β”€β”€β”€β”˜β”Œβ”€β”΄β”€β”     
q_6: ─────────────────────────────── X β”œβ”€β”€β– β”€β”€
                                   β””β”€β”€β”€β”˜β”Œβ”€β”΄β”€β”
q_7: ──────────────────────────────────── X β”œ
                                        β””β”€β”€β”€β”˜
sample03 = QuantumCircuit(8)
sample03.x(range(0, 8, 2))
print("| Custom circuit:")
print(sample03)
| Custom circuit:
     β”Œβ”€β”€β”€β”
q_0: ─ X β”œ
     β””β”€β”€β”€β”˜
q_1: ─────
     β”Œβ”€β”€β”€β”
q_2: ─ X β”œ
     β””β”€β”€β”€β”˜
q_3: ─────
     β”Œβ”€β”€β”€β”
q_4: ─ X β”œ
     β””β”€β”€β”€β”˜
q_5: ─────
     β”Œβ”€β”€β”€β”
q_6: ─ X β”œ
     β””β”€β”€β”€β”˜
q_7: ─────
          

c. Execute the circuitΒΆ

i. Directly input the circuitΒΆ

After executing, it will return a uuid of experiment. You can use this uuid to get the result of the experiment.

exp1 = experiment_randomized.measure(sample01, times=100, shots=4096)
exp1
'd3bf5830-5a87-4f7a-b999-a0d636c6cb07'

Each experiment result will be stored in a container .exps.

experiment_randomized.exps[exp1]
<EntropyMeasureRandomizedExperiment(exp_id=d3bf5830-5a87-4f7a-b999-a0d636c6cb07, 
  EntropyMeasureRandomizedArguments(exp_name='experiment.N_U_100.qurrent_randomized', times=100, qubits_measured=[0, 1, 2, 3, 4, 5, 6, 7], registers_mapping={0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7}, actual_num_qubits=8, unitary_located=[0, 1, 2, 3, 4, 5, 6, 7], random_unitary_seeds=None),
  Commonparams(exp_id='d3bf5830-5a87-4f7a-b999-a0d636c6cb07', target_keys=[0], shots=4096, backend=<AerSimulator('aer_simulator')>, run_args={}, transpile_args={}, tags=(), save_location=PosixPath('.'), serial=None, summoner_id=None, summoner_name=None, datetimes=DatetimeDict({'build': '2025-07-08 16:59:41', 'run.001': '2025-07-08 16:59:41'})),
  unused_args_num=0,
  analysis_num=0))>

And use this uuid to access the experiments to execute post-processing.

report01 = experiment_randomized.exps[exp1].analyze(
    selected_qubits=[0, 1, 2, 3],
)
report01
<EMRAnalysis(
  serial=0,
  EMRAnalysisInput(num_qubits=8, selected_qubits=[0, 1, 2, 3], registers_mapping={0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7}, bitstring_mapping={0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7}, shots=4096, unitary_located=[0, 1, 2, 3, 4, 5, 6, 7]),
  EMRAnalysisContent(purity=1.0818525683879852, entropy=-0.11350390642180241, and others)),
  unused_args_num=0
  )>
main01, side_product01 = report01.export()
main01
{'purity': np.float64(1.0818525683879852),
 'entropy': np.float64(-0.11350390642180241),
 'puritySD': np.float64(0.9047121303994405),
 'entropySD': np.float64(1.2064709574098542),
 'num_classical_registers': 8,
 'classical_registers': [0, 1, 2, 3],
 'classical_registers_actually': [0, 1, 2, 3],
 'all_system_source': 'independent',
 'purityAllSys': np.float64(0.9814594411849975),
 'entropyAllSys': np.float64(0.026999445341914383),
 'puritySDAllSys': np.float64(1.2117104526181024),
 'entropySDAllSys': np.float64(1.7811522184501039),
 'num_classical_registers_all_sys': 8,
 'classical_registers_all_sys': None,
 'classical_registers_actually_all_sys': [0, 1, 2, 3, 4, 5, 6, 7],
 'errorRate': np.float64(0.009350347947765595),
 'mitigatedPurity': np.float64(1.1011859063770018),
 'mitigatedEntropy': np.float64(-0.13905805070709734),
 'counts_num': 100,
 'taking_time': 0.001137362,
 'taking_time_all_sys': 0.010858034,
 'counts_used': None,
 'input': {'num_qubits': 8,
  'selected_qubits': [0, 1, 2, 3],
  'registers_mapping': {0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7},
  'bitstring_mapping': {0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7},
  'shots': 4096,
  'unitary_located': [0, 1, 2, 3, 4, 5, 6, 7]},
 'header': {'serial': 0, 'datetime': '2025-07-08 16:59:42', 'log': {}}}

ii. Add the circuits to container .waves, then call them later.ΒΆ

Since we have executed an experiment, the circuit we input in exp1 is stored in the container .waves with serial number 0.

experiment_randomized.waves
WaveContainer({
  0: <qurry.recipe.simple.paramagnet.TrivialParamagnet object at 0x700e05c21130>})

But we can also add the circuit to the container .waves with a custom name. The name should be unique, otherwise it will be overwritten. The method add will return the actual name of the circuit in the container.

print(experiment_randomized.add(sample02, "ghz_8"))
print(experiment_randomized.waves["ghz_8"])
ghz_8
     β”Œβ”€β”€β”€β”                                   
q_0: ─ H β”œβ”€β”€β– β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
     β””β”€β”€β”€β”˜β”Œβ”€β”΄β”€β”                              
q_1: ────── X β”œβ”€β”€β– β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
          β””β”€β”€β”€β”˜β”Œβ”€β”΄β”€β”                         
q_2: ─────────── X β”œβ”€β”€β– β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
               β””β”€β”€β”€β”˜β”Œβ”€β”΄β”€β”                    
q_3: ──────────────── X β”œβ”€β”€β– β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
                    β””β”€β”€β”€β”˜β”Œβ”€β”΄β”€β”               
q_4: ───────────────────── X β”œβ”€β”€β– β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
                         β””β”€β”€β”€β”˜β”Œβ”€β”΄β”€β”          
q_5: ────────────────────────── X β”œβ”€β”€β– β”€β”€β”€β”€β”€β”€β”€
                              β””β”€β”€β”€β”˜β”Œβ”€β”΄β”€β”     
q_6: ─────────────────────────────── X β”œβ”€β”€β– β”€β”€
                                   β””β”€β”€β”€β”˜β”Œβ”€β”΄β”€β”
q_7: ──────────────────────────────────── X β”œ
                                        β””β”€β”€β”€β”˜

If there is a circuit with the same name, it will be replaced by the new one.

print(experiment_randomized.add(sample03, "ghz_8"))
print(experiment_randomized.waves["ghz_8"])
ghz_8
     β”Œβ”€β”€β”€β”
q_0: ─ X β”œ
     β””β”€β”€β”€β”˜
q_1: ─────
     β”Œβ”€β”€β”€β”
q_2: ─ X β”œ
     β””β”€β”€β”€β”˜
q_3: ─────
     β”Œβ”€β”€β”€β”
q_4: ─ X β”œ
     β””β”€β”€β”€β”˜
q_5: ─────
     β”Œβ”€β”€β”€β”
q_6: ─ X β”œ
     β””β”€β”€β”€β”˜
q_7: ─────
          

Otherwise, you will need to use replace="duplicate" to prevent it from being replaced.

duplicated_case01 = experiment_randomized.add(sample02, "ghz_8", replace="duplicate")
print(duplicated_case01)
print(experiment_randomized.waves[duplicated_case01])
ghz_8.2
     β”Œβ”€β”€β”€β”                                   
q_0: ─ H β”œβ”€β”€β– β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
     β””β”€β”€β”€β”˜β”Œβ”€β”΄β”€β”                              
q_1: ────── X β”œβ”€β”€β– β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
          β””β”€β”€β”€β”˜β”Œβ”€β”΄β”€β”                         
q_2: ─────────── X β”œβ”€β”€β– β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
               β””β”€β”€β”€β”˜β”Œβ”€β”΄β”€β”                    
q_3: ──────────────── X β”œβ”€β”€β– β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
                    β””β”€β”€β”€β”˜β”Œβ”€β”΄β”€β”               
q_4: ───────────────────── X β”œβ”€β”€β– β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
                         β””β”€β”€β”€β”˜β”Œβ”€β”΄β”€β”          
q_5: ────────────────────────── X β”œβ”€β”€β– β”€β”€β”€β”€β”€β”€β”€
                              β””β”€β”€β”€β”˜β”Œβ”€β”΄β”€β”     
q_6: ─────────────────────────────── X β”œβ”€β”€β– β”€β”€
                                   β””β”€β”€β”€β”˜β”Œβ”€β”΄β”€β”
q_7: ──────────────────────────────────── X β”œ
                                        β””β”€β”€β”€β”˜

Now we have prepared the circuit and stored it in the container .waves.

experiment_randomized.waves
WaveContainer({
  0: <qurry.recipe.simple.paramagnet.TrivialParamagnet object at 0x700e05c21130>,
  'ghz_8': <qiskit.circuit.quantumcircuit.QuantumCircuit object at 0x700e05afcc80>,
  'ghz_8.2': <qurry.recipe.simple.cat.GHZ object at 0x700e05afc3e0>})

Finally, we can execute the circuit and get the result.

exp2 = experiment_randomized.measure("ghz_8.2", times=100, shots=4096)
exp2
'6c8ad942-ec28-4d7c-80bf-9f0875fb7540'
experiment_randomized.exps[exp2]
<EntropyMeasureRandomizedExperiment(exp_id=6c8ad942-ec28-4d7c-80bf-9f0875fb7540, 
  EntropyMeasureRandomizedArguments(exp_name='experiment.N_U_100.qurrent_randomized', times=100, qubits_measured=[0, 1, 2, 3, 4, 5, 6, 7], registers_mapping={0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7}, actual_num_qubits=8, unitary_located=[0, 1, 2, 3, 4, 5, 6, 7], random_unitary_seeds=None),
  Commonparams(exp_id='6c8ad942-ec28-4d7c-80bf-9f0875fb7540', target_keys=['ghz_8.2'], shots=4096, backend=<AerSimulator('aer_simulator')>, run_args={}, transpile_args={}, tags=(), save_location=PosixPath('.'), serial=None, summoner_id=None, summoner_name=None, datetimes=DatetimeDict({'build': '2025-07-08 17:00:07', 'run.001': '2025-07-08 17:00:07'})),
  unused_args_num=0,
  analysis_num=0))>
report02 = experiment_randomized.exps[exp2].analyze(
    selected_qubits=[0, 1, 2, 3],
)
report02
<EMRAnalysis(
  serial=0,
  EMRAnalysisInput(num_qubits=8, selected_qubits=[0, 1, 2, 3], registers_mapping={0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7}, bitstring_mapping={0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7}, shots=4096, unitary_located=[0, 1, 2, 3, 4, 5, 6, 7]),
  EMRAnalysisContent(purity=0.45413352966308596, entropy=1.138811536889995, and others)),
  unused_args_num=0
  )>

d. Export them after allΒΆ

exp1_id, exp1_files_info = experiment_randomized.exps[exp1].write(
    save_location=".",  # where to save files
)
exp1_files_info
{'folder': 'experiment.N_U_100.qurrent_randomized.001',
 'qurryinfo': 'experiment.N_U_100.qurrent_randomized.001/qurryinfo.json',
 'args': 'experiment.N_U_100.qurrent_randomized.001/args/experiment.N_U_100.qurrent_randomized.001.id=d3bf5830-5a87-4f7a-b999-a0d636c6cb07.args.json',
 'advent': 'experiment.N_U_100.qurrent_randomized.001/advent/experiment.N_U_100.qurrent_randomized.001.id=d3bf5830-5a87-4f7a-b999-a0d636c6cb07.advent.json',
 'legacy': 'experiment.N_U_100.qurrent_randomized.001/legacy/experiment.N_U_100.qurrent_randomized.001.id=d3bf5830-5a87-4f7a-b999-a0d636c6cb07.legacy.json',
 'tales.unitaryOP': 'experiment.N_U_100.qurrent_randomized.001/tales/experiment.N_U_100.qurrent_randomized.001.id=d3bf5830-5a87-4f7a-b999-a0d636c6cb07.unitaryOP.json',
 'tales.randomized': 'experiment.N_U_100.qurrent_randomized.001/tales/experiment.N_U_100.qurrent_randomized.001.id=d3bf5830-5a87-4f7a-b999-a0d636c6cb07.randomized.json',
 'reports': 'experiment.N_U_100.qurrent_randomized.001/reports/experiment.N_U_100.qurrent_randomized.001.id=d3bf5830-5a87-4f7a-b999-a0d636c6cb07.reports.json',
 'reports.tales.purityCells': 'experiment.N_U_100.qurrent_randomized.001/tales/experiment.N_U_100.qurrent_randomized.001.id=d3bf5830-5a87-4f7a-b999-a0d636c6cb07.purityCells.reports.json',
 'reports.tales.purityCellsAllSys': 'experiment.N_U_100.qurrent_randomized.001/tales/experiment.N_U_100.qurrent_randomized.001.id=d3bf5830-5a87-4f7a-b999-a0d636c6cb07.purityCellsAllSys.reports.json'}

Post-Process Availablities and Version InfoΒΆ

from qurry.process import AVAIBILITY_STATESHEET

AVAIBILITY_STATESHEET
 | Qurrium version: 0.13.0
---------------------------------------------------------------------------
 ### Qurrium Post-Processing
   - Backend Availability ................... Python Cython Rust   JAX   
 - randomized_measure
   - entangled_entropy.entropy_core_2 ....... Yes    Depr.  Yes    No    
   - entangle_entropy.purity_cell_2 ......... Yes    Depr.  Yes    No    
   - entangled_entropy_v1.entropy_core ...... Yes    Depr.  Yes    No    
   - entangle_entropy_v1.purity_cell ........ Yes    Depr.  Yes    No    
   - wavefunction_overlap.echo_core_2 ....... Yes    Depr.  Yes    No    
   - wavefunction_overlap.echo_cell_2 ....... Yes    Depr.  Yes    No    
   - wavefunction_overlap_v1.echo_core ...... Yes    Depr.  Yes    No    
   - wavefunction_overlap_v1.echo_cell ...... Yes    Depr.  Yes    No    
 - hadamard_test
   - purity_echo_core ....................... Yes    No     Yes    No    
 - magnet_square
   - magnsq_core ............................ Yes    No     Yes    No    
 - string_operator
   - strop_core ............................. Yes    No     Yes    No    
 - classical_shadow
   - rho_m_core ............................. Yes    No     No     Yes   
 - utils
   - randomized ............................. Yes    Depr.  Yes    No    
   - counts_process ......................... Yes    No     Yes    No    
   - bit_slice .............................. Yes    No     Yes    No    
   - dummy .................................. Yes    No     Yes    No    
   - test ................................... Yes    No     Yes    No    
---------------------------------------------------------------------------
   + Yes ...... Working normally.
   + Error .... Exception occurred.
   + No ....... Not supported.
   + Depr. .... Deprecated.
---------------------------------------------------------------------------
by <Hoshi>