3.2 Magnetization Square for Any Directions#
Basic Usage#
a. Import the instances#
from qurry import MagnetSquare
experiment_magnetsquare = MagnetSquare()
b. Preparing quantum circuit#
from qurry.recipe import Cat, TrivialParamagnet
circuits_dict = {
"trivialPM_2": TrivialParamagnet(2),
"trivialPM_4": TrivialParamagnet(4),
"trivialPM_6": TrivialParamagnet(6),
"trivialPM_8": TrivialParamagnet(8),
"cat_2": Cat(2),
"cat_4": Cat(4),
"cat_6": Cat(6),
"cat_8": Cat(8),
}
print("| trivial paramagnet and cat in 4 qubits:")
print(circuits_dict["trivialPM_4"])
print(circuits_dict["cat_4"])
| trivial paramagnet and cat in 4 qubits:
┌───┐
q_0: ┤ H ├
├───┤
q_1: ┤ H ├
├───┤
q_2: ┤ H ├
├───┤
q_3: ┤ H ├
└───┘
┌───┐
q_0: ┤ H ├──■────────────
└───┘┌─┴─┐
q_1: ─────┤ X ├──■───────
└───┘┌─┴─┐
q_2: ──────────┤ X ├──■──
└───┘┌─┴─┐
q_3: ───────────────┤ X ├
└───┘
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_magnetsquare.measure(
circuits_dict["cat_6"], unitary_operator="z", shots=4096
)
exp1
'c73fd098-1ab6-43c6-811e-d32ca410843b'
Each experiment result will be stored in a container .exps
.
experiment_magnetsquare.exps[exp1]
<MagnetSquareExperiment(exp_id=c73fd098-1ab6-43c6-811e-d32ca410843b,
MagnetSquareArguments(exp_name='experiment.qurmagsq_magnet_square', num_qubits=6, unitary_operator='z'),
Commonparams(exp_id='c73fd098-1ab6-43c6-811e-d32ca410843b', 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-06-26 11:44:42', 'run.001': '2025-06-26 11:44:42'})),
unused_args_num=0,
analysis_num=1))>
For MagnetSquare
, its .analyze
in MagnetSquareExperiment
does not require any arguments, so its post-processing will be executed automatically after .measure
.
experiment_magnetsquare.exps[exp1].reports
AnalysisContainer(length=1, {
0: <MSAnalysis(serial=0, MSAnalysisInput(), MSAnalysisContent(magnet_square=1.0, unitary_operator=z, and others)), unused_args_num=0>})
report01 = experiment_magnetsquare.exps[exp1].reports[0]
report01
<MSAnalysis(
serial=0,
MSAnalysisInput(),
MSAnalysisContent(magnet_square=1.0, unitary_operator=z, and others)),
unused_args_num=0
)>
main01, side_product01 = report01.export()
print("| magnetization square", main01["magnet_square"])
for k, v in main01.items():
if "magnet_square" in k:
continue
print(f"| {k}: {v}")
| magnetization square 1.0
| num_qubits: 6
| shots: 4096
| unitary_operator: z
| taking_time: 0.00049866
| input: {}
| header: {'serial': 0, 'datetime': '2025-06-26 11:44:42', 'log': {}}
print("| side product is empty here:", side_product01)
| side product is empty here: {}
main
contains another keys "magnet_square_cells"
which is a dict,
It contains the magnetization square cells they correspond to the magnetization square.
print(main01["magnet_square_cells"])
{17: 1.0, 25: 1.0, 23: 1.0, 1: 1.0, 21: 1.0, 28: 1.0, 22: 1.0, 26: 1.0, 4: 1.0, 16: 1.0, 0: 1.0, 27: 1.0, 12: 1.0, 7: 1.0, 24: 1.0, 9: 1.0, 2: 1.0, 6: 1.0, 3: 1.0, 13: 1.0, 10: 1.0, 11: 1.0, 18: 1.0, 20: 1.0, 8: 1.0, 15: 1.0, 19: 1.0, 29: 1.0, 14: 1.0, 5: 1.0}
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_magnetsquare.waves
WaveContainer({ 0: <qurry.recipe.simple.cat.Cat object at 0x70fde8c8b250>})
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.
for k, v in circuits_dict.items():
key_of_cirq = experiment_magnetsquare.add(v, k)
print(f"| {k} added with key: {key_of_cirq}")
print(experiment_magnetsquare.waves["cat_4"])
| trivialPM_2 added with key: trivialPM_2
| trivialPM_4 added with key: trivialPM_4
| trivialPM_6 added with key: trivialPM_6
| trivialPM_8 added with key: trivialPM_8
| cat_2 added with key: cat_2
| cat_4 added with key: cat_4
| cat_6 added with key: cat_6
| cat_8 added with key: cat_8
┌───┐
q_0: ┤ H ├──■────────────
└───┘┌─┴─┐
q_1: ─────┤ X ├──■───────
└───┘┌─┴─┐
q_2: ──────────┤ X ├──■──
└───┘┌─┴─┐
q_3: ───────────────┤ X ├
└───┘
If there is a circuit with the same name, it will be replaced by the new one.
print(experiment_magnetsquare.add(circuits_dict["cat_4"], "cat_4"))
print(experiment_magnetsquare.waves["cat_4"])
cat_4
┌───┐
q_0: ┤ H ├──■────────────
└───┘┌─┴─┐
q_1: ─────┤ X ├──■───────
└───┘┌─┴─┐
q_2: ──────────┤ X ├──■──
└───┘┌─┴─┐
q_3: ───────────────┤ X ├
└───┘
Otherwise, you will need to use replace="duplicate"
to prevent it from being replaced.
duplicated_case01 = experiment_magnetsquare.add(
circuits_dict["cat_4"], "cat_4", replace="duplicate"
)
print(duplicated_case01)
print(experiment_magnetsquare.waves[duplicated_case01])
cat_4.9
┌───┐
q_0: ┤ H ├──■────────────
└───┘┌─┴─┐
q_1: ─────┤ X ├──■───────
└───┘┌─┴─┐
q_2: ──────────┤ X ├──■──
└───┘┌─┴─┐
q_3: ───────────────┤ X ├
└───┘
Now we have prepared the circuit and stored it in the container .waves
.
experiment_magnetsquare.waves
WaveContainer({
0: <qurry.recipe.simple.cat.Cat object at 0x70fde8c8b250>,
'trivialPM_2': <qurry.recipe.simple.paramagnet.TrivialParamagnet object at 0x70fde8bf86e0>,
'trivialPM_4': <qurry.recipe.simple.paramagnet.TrivialParamagnet object at 0x70fde8c8ae90>,
'trivialPM_6': <qurry.recipe.simple.paramagnet.TrivialParamagnet object at 0x70fde8c8afd0>,
'trivialPM_8': <qurry.recipe.simple.paramagnet.TrivialParamagnet object at 0x70fe3dd336f0>,
'cat_2': <qurry.recipe.simple.cat.Cat object at 0x70fde8bf8830>,
'cat_4': <qurry.recipe.simple.cat.Cat object at 0x70fde8c8b110>,
'cat_6': <qurry.recipe.simple.cat.Cat object at 0x70fde8c8b250>,
'cat_8': <qurry.recipe.simple.cat.Cat object at 0x70fde8bad5b0>,
'cat_4.9': <qurry.recipe.simple.cat.Cat object at 0x70fde8c8b110>})
Finally, we can execute the circuit and get the result.
exp2 = experiment_magnetsquare.measure("cat_4", unitary_operator="z", shots=4096)
exp2
'03b0a78b-77ea-45ee-bad5-1481dc2ae595'
experiment_magnetsquare.exps[exp2]
<MagnetSquareExperiment(exp_id=03b0a78b-77ea-45ee-bad5-1481dc2ae595,
MagnetSquareArguments(exp_name='experiment.qurmagsq_magnet_square', num_qubits=4, unitary_operator='z'),
Commonparams(exp_id='03b0a78b-77ea-45ee-bad5-1481dc2ae595', target_keys=['cat_4'], 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-06-26 11:44:48', 'run.001': '2025-06-26 11:44:48'})),
unused_args_num=0,
analysis_num=1))>
report02 = experiment_magnetsquare.exps[exp2].reports[0]
report02
<MSAnalysis(
serial=0,
MSAnalysisInput(),
MSAnalysisContent(magnet_square=1.0, unitary_operator=z, and others)),
unused_args_num=0
)>
d. More direction Consider#
MagnetSquare
also can measure on different direction. Except x
, y
, z
have been preset. You can use other single qubit instruction like Gate
from qiskit.circuit
or Operator
from qiskit.quantum_info
to set specific direction you wanted.
For example, we use a RZGate
on $\phi$ = 0 which is identity and its Operator
version.
from qiskit.circuit.library import RZGate
from qiskit.quantum_info import Operator
Preparing RZGate
and its Operator
version.
rz_0 = RZGate(0)
rz_0_matrix = rz_0.to_matrix()
rz_0_matrix
array([[1.-0.j, 0.+0.j],
[0.+0.j, 1.+0.j]])
rz_0_op = Operator(rz_0_matrix)
rz_0_op
Operator([[1.-0.j, 0.+0.j],
[0.+0.j, 1.+0.j]],
input_dims=(2,), output_dims=(2,))
Executing the experiments
exp_rz_gate = experiment_magnetsquare.measure(
"cat_4", unitary_operator=rz_0, shots=4096
)
reports_rz_gate = experiment_magnetsquare.exps[exp_rz_gate].reports[0]
main_rz_gate, _side_product_rz_gate = reports_rz_gate.export()
print("| magnetization square using RZGate")
print("|" + "-" * 50)
print("| magnetization square", main_rz_gate["magnet_square"])
for k, v in main_rz_gate.items():
if "magnet_square" in k:
continue
print(f"| {k}: {v}")
reports_rz_gate
| magnetization square using RZGate
|--------------------------------------------------
| magnetization square 1.0
| num_qubits: 4
| shots: 4096
| unitary_operator: [[1.-0.j 0.+0.j]
[0.+0.j 1.+0.j]]
| taking_time: 0.000521005
| input: {}
| header: {'serial': 0, 'datetime': '2025-06-26 11:44:54', 'log': {}}
<MSAnalysis(
serial=0,
MSAnalysisInput(),
MSAnalysisContent(magnet_square=1.0, unitary_operator=[[1.-0.j 0.+0.j]
[0.+0.j 1.+0.j]], and others)),
unused_args_num=0
)>
exp_rz_op = experiment_magnetsquare.measure(
"cat_4", unitary_operator=rz_0_op, shots=4096
)
reports_rz_op = experiment_magnetsquare.exps[exp_rz_op].reports[0]
main_rz_op, _side_product_rz_op = reports_rz_op.export()
print("| magnetization square using RZGate Operator")
print("|" + "-" * 50)
print("| magnetization square", main_rz_op["magnet_square"])
for k, v in main_rz_op.items():
if "magnet_square" in k:
continue
print(f"| {k}: {v}")
reports_rz_op
| magnetization square using RZGate Operator
|--------------------------------------------------
| magnetization square 1.0
| num_qubits: 4
| shots: 4096
| unitary_operator: [[1.-0.j 0.+0.j]
[0.+0.j 1.+0.j]]
| taking_time: 0.000560065
| input: {}
| header: {'serial': 0, 'datetime': '2025-06-26 11:45:00', 'log': {}}
<MSAnalysis(
serial=0,
MSAnalysisInput(),
MSAnalysisContent(magnet_square=1.0, unitary_operator=[[1.-0.j 0.+0.j]
[0.+0.j 1.+0.j]], and others)),
unused_args_num=0
)>
e. Export them after all#
exp1_id, exp1_files_info = experiment_magnetsquare.exps[exp1].write(
save_location=".", # where to save files
)
exp1_files_info
{'folder': 'experiment.qurmagsq_magnet_square.001',
'qurryinfo': 'experiment.qurmagsq_magnet_square.001/qurryinfo.json',
'args': 'experiment.qurmagsq_magnet_square.001/args/experiment.qurmagsq_magnet_square.001.id=c73fd098-1ab6-43c6-811e-d32ca410843b.args.json',
'advent': 'experiment.qurmagsq_magnet_square.001/advent/experiment.qurmagsq_magnet_square.001.id=c73fd098-1ab6-43c6-811e-d32ca410843b.advent.json',
'legacy': 'experiment.qurmagsq_magnet_square.001/legacy/experiment.qurmagsq_magnet_square.001.id=c73fd098-1ab6-43c6-811e-d32ca410843b.legacy.json',
'reports': 'experiment.qurmagsq_magnet_square.001/reports/experiment.qurmagsq_magnet_square.001.id=c73fd098-1ab6-43c6-811e-d32ca410843b.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>