paddle_quantum.mbqc.simulator
This module contains the commonly used class and simulation tools in MBQC.
- class paddle_quantum.mbqc.simulator.MBQC
Bases:
object
Define a
MBQC
class used for measurement based quantum computation.The users can define their MBQC models by instantiate the objects of this class.
- set_graph(graph)
Set the graphs in MBQC model.
The users can use this function to transport their own graphs to the
MBQC
object.- Parameters:
graph – The graphs in MBQC model. The list takes the form
[V, E]
, whereV
are nodes, andE
are edges.
- get_graph()
Get the information of graphs.
- Returns:
graph
- Return type:
nx.Graph
- set_pattern(pattern)
Set the measurement patterns of the MBQC model.
This function is used for transport the measurement patterns to
MBQC
object. The measurement patterns are acquired by either translation from the quantum circuit or the construction of users.Warning
The input pattern parameter is of type
Pattern
, in which the command list containsEMC
commands.- Parameters:
pattern (Pattern) – The measurement patterns corresponding to the MBQC algorithms.
- get_pattern()
Get the information of the measurement patterns.
- Returns:
measurement pattern
- Return type:
Pattern
- set_input_state(state=None)
Set the input state to be replaced.
Warning
Unlike the circuit model, the initial state of MBQC model is |+> state. If the users don’t use this method to initialize the quantum state, the initial state will be |+> state. If the users run the MBQC model in measurement mode, the system labels of the input state here will be restricted to natural number(start from 0) of
int
type.- Parameters:
state – the input state to be replaced, the default is |+> state.
- draw_process(draw=True, pos=False, pause_time=0.5)
Dynamically plot the computation process of the MBQC model.
- Parameters:
draw (bool, optional) – The boolean switch of whether plot the computation process.
pos (bool or dict, optional) – the dict of the nodes’ labels or built-in choice of coordinates. The built-in choice of coordinates are:
True
is the coordinates of the measurement patterns,False
is thespring_layout
coordinates.pause_time (float, optional) – The time step for updating the picture.
- track_progress(track=True)
A switch for whether showing the progress bar of MBQC computation process.
- Parameters:
track (bool, optional) –
True
open the progress bar,False
close the progress bar.
- measure(which_qubit, basis_list)
Measure given qubits with given measurement basis.
Note
This is one of the most common methods we use after instantiating an MBQC object. Here we optimize the single bit measurement simulation to the maximum extent. Once use this method, the MBQC class will automatically activate the related nodes, generate the corresponding graph state, measure specific qubits and store the results of the numerical simulations.
Warning
If and only if the users use this method, the MBQC model carries out the computation.
- Parameters:
which_qubit (any) – The system labels of the qubit to be measured. Any type (e.g.
str
,tuple
) can be used, as long as the type matches the type of labels of the nodes in MBQC model.basis_list (list) – a list composed of measurement basis, the elements are column vectors of type
Tensor
.
Code example:
from paddle_quantum.mbqc.simulator import MBQC from paddle_quantum.mbqc.qobject import State from paddle_quantum.mbqc.utils import zero_state, basis G = [['1', '2', '3'], [('1', '2'), ('2', '3')]] mbqc = MBQC() mbqc.set_graph(G) state = State(zero_state(), ['1']) mbqc.set_input_state(state) mbqc.measure('1', basis('X')) mbqc.measure('2', basis('X')) print("Measurement outcomes: ", mbqc.get_classical_output())
Measurement outcomes: {'1': 0, '2': 1}
- sum_outcomes(which_qubits, start=0)
Based on the input system labels, find the corresponding measurement results in the dict and sum over.
Note
When correcting the byproduct or defining the angle of adaptive measurement, one can use this method to sum over the measurement results of given qubits.
- Parameters:
which_qubits (list) – the list contains the system labels of the nodes whose measurement results should be find out and summed over.
start (int) – an extra integer added to the results.
- Returns:
The sum of the measurement results of given qubit.
- Return type:
int
Code example:
from paddle_quantum.mbqc.simulator import MBQC from paddle_quantum.mbqc.qobject import State from paddle_quantum.mbqc.utils import zero_state, basis G = [['1', '2', '3'], [('1', '2'), ('2', '3')]] mbqc = MBQC() mbqc.set_graph(G) input_state = State(zero_state(), ['1']) mbqc.set_input_state(input_state) mbqc.measure('1', basis('X')) mbqc.measure('2', basis('X')) mbqc.measure('3', basis('X')) print("All measurement outcomes: ", mbqc.get_classical_output()) print("Sum of outcomes of qubits '1' and '2': ", mbqc.sum_outcomes(['1', '2'])) print("Sum of outcomes of qubits '1', '2' and '3' with an extra 1: ", mbqc.sum_outcomes(['1', '2', '3'], 1))
All measurement outcomes: {'1': 0, '2': 0, '3': 1} Sum of outcomes of qubits '1' and '2': 0 Sum of outcomes of qubits '1', '2' and '3' with an extra 1: 2
- correct_byproduct(gate, which_qubit, power)
Correct the byproduct of the measured quantum state.
Note
This is a commonly used method after the measurement process of the MBQC model.
- Parameters:
gate (str) –
'X'
or'Z'
, representing Pauli X or Pauli Z correction respectively. which_qubit (any): The system labels of the qubit to be processed. Any type (e.g.str
,tuple
) can be used, as long as the type matches the type of labels of the nodes in MBQC model.power (int) – the index of the byproduct correcting.
- Code example:
Here is an example of quantum teleportation in MBQC framework.
from paddle_quantum.mbqc.simulator import MBQC from paddle_quantum.mbqc.qobject import State from paddle_quantum.mbqc.utils import random_state_vector, basis, compare_by_vector G = [['1', '2', '3'], [('1', '2'), ('2', '3')]] state = State(random_state_vector(1), ['1']) mbqc = MBQC() mbqc.set_graph(G) mbqc.set_input_state(state) mbqc.measure('1', basis('X')) mbqc.measure('2', basis('X')) outcome = mbqc.get_classical_output() mbqc.correct_byproduct('Z', '3', outcome['1']) mbqc.correct_byproduct('X', '3', outcome['2']) state_out = mbqc.get_quantum_output() state_std = State(state.vector, ['3']) compare_by_vector(state_out, state_std)
Norm difference of the given states is: 0.0 They are exactly the same states.
- run_pattern()
Run the MBQC model by the measurement patterns set before.
Warning
This method must be used after
set_pattern
.
- get_classical_output()
Get the classical output of the MBQC model.
- Returns:
if the users input the measurement patterns, the method returns the bit strings of the measurement results of the output qubits which is the same as the results of the circuit based model. The qubits haven’t been measured fills “?”. If the input is graph, return the measurement results of all nodes.
- Return type:
str or dict
- get_history()
Get the information during the MBQC computation process.
- Returns:
the list of the results, including generate graph state, measurement and byproduct correction.
- Return type:
list
- paddle_quantum.mbqc.simulator.simulate_by_mbqc(circuit, input_state=None)
Simulate the quantum circuit by equivalent MBQC model.
This function transform the quantum circuit to equivalent MBQC models and acquire output equivalent to the circuit based model.
Warning
Unlike the
UAnsatz
, the inputcircuit
here contains the measurement operations. By the way, if the users setinput_state=None
, the initial state of the MBQC is |+> state.