Pattern Simulation¶
graphix.simulator module¶
- class graphix.simulator.PatternSimulator(pattern, backend='statevector', noise_model=None, **kwargs)[source]¶
MBQC simulator
Executes the measurement pattern.
- __init__(pattern, backend='statevector', noise_model=None, **kwargs)[source]¶
- Parameters:
pattern (
graphix.pattern.Patternobject) – MBQC pattern to be simulated.backend (str, 'statevector', 'densitymatrix or 'tensornetwork') – simulation backend (optional), default is ‘statevector’.
noise_model
kwargs (keyword args for specified backend.)
seealso: (..) –
graphix.sim.statevec.StatevectorBackendgraphix.sim.tensornet.TensorNetworkBackendgraphix.sim.density_matrix.DensityMatrixBackend:
Simulator backends¶
Tensor Network¶
- class graphix.sim.tensornet.TensorNetworkBackend(pattern, graph_prep='auto', input_state=PlanarState object defined in plane Plane.XY with angle 0.0., **kwargs)[source]¶
Tensor Network Simulator for MBQC
Executes the measurement pattern using TN expression of graph states.
- __init__(pattern, graph_prep='auto', input_state=PlanarState object defined in plane Plane.XY with angle 0.0., **kwargs)[source]¶
- Parameters:
pattern (graphix.Pattern)
graph_prep (str) –
- ‘parallel’ :
Faster method for preparing a graph state. The expression of a graph state can be obtained from the graph geometry. See https://journals.aps.org/pra/abstract/10.1103/PhysRevA.76.052315 for detail calculation. Note that ‘N’ and ‘E’ commands in the measurement pattern are ignored.
- ’sequential’ :
Sequentially execute N and E commands, strictly following the measurement pattern. In this strategy, All N and E commands executed sequentially.
- ’auto’(default) :
Automatically select a preparation strategy based on the max degree of a graph
input_state (preparation for input states (only BasicStates.PLUS is supported for tensor networks yet),)
**kwargs (Additional keyword args to be passed to quimb.tensor.TensorNetwork.)
- add_nodes(nodes)[source]¶
Add nodes to the network
- Parameters:
nodes (iterator of int) – index set of the new nodes.
- apply_clifford(cmd: C)[source]¶
Apply single-qubit Clifford gate
- Parameters:
cmd (list) – clifford command. See https://arxiv.org/pdf/2212.11975.pdf for the detail.
- correct_byproduct(cmd: X | Z)[source]¶
Perform byproduct correction.
- Parameters:
cmd (list) – Byproduct command i.e. [‘X’ or ‘Z’, node, signal_domain]
- entangle_nodes(edge)[source]¶
Make entanglement between nodes specified by edge.
- Parameters:
edge (tuple of int) – edge specifies two target nodes of the CZ gate.
- measure(cmd: M)[source]¶
Perform measurement of the node. In the context of tensornetwork, performing measurement equals to applying measurement operator to the tensor. Here, directly contracted with the projected state.
- Parameters:
cmd (list) – measurement command i.e. [‘M’, node, plane angle, s_domain, t_domain]
Statevector¶
- class graphix.sim.statevec.StatevectorBackend(pattern, input_state: ~graphix.states.State | ~graphix.sim.statevec.Statevec | ~collections.abc.Iterable[~graphix.states.State] | ~collections.abc.Iterable[~numbers.Number] = PlanarState object defined in plane Plane.XY with angle 0.0., max_qubit_num=20, pr_calc=True, rng=None)[source]¶
MBQC simulator with statevector method.
- __init__(pattern, input_state: ~graphix.states.State | ~graphix.sim.statevec.Statevec | ~collections.abc.Iterable[~graphix.states.State] | ~collections.abc.Iterable[~numbers.Number] = PlanarState object defined in plane Plane.XY with angle 0.0., max_qubit_num=20, pr_calc=True, rng=None)[source]¶
- Parameters:
pattern (
graphix.pattern.Patternobject) – MBQC pattern to be simulated.input_state (same syntax as graphix.statevec.Statevec constructor.)
max_qubit_num (int) – optional argument specifying the maximum number of qubits to be stored in the statevector at a time.
pr_calc (bool) – whether or not to compute the probability distribution before choosing the measurement result. if False, measurements yield results 0/1 with 50% probabilities each.
- add_nodes(nodes: list[int], input_state=PlanarState object defined in plane Plane.XY with angle 0.0.) None[source]¶
add new qubit to internal statevector and assign the corresponding node number to list self.node_index.
- Parameters:
nodes (list of node indices)
- apply_clifford(cmd: C)[source]¶
Apply single-qubit Clifford gate, specified by vop index specified in graphix.clifford.CLIFFORD
- correct_byproduct(cmd: list[X, Z])[source]¶
Byproduct correction correct for the X or Z byproduct operators, by applying the X or Z gate.
- entangle_nodes(edge: tuple[int])[source]¶
Apply CZ gate to two connected nodes
- Parameters:
edge (tuple (i, j)) – a pair of node indices
- measure(cmd: M)[source]¶
Perform measurement of a node in the internal statevector and trace out the qubit
- Parameters:
cmd (list) – measurement command : [‘M’, node, plane angle, s_domain, t_domain]
- graphix.sim.statevec.meas_op(angle, vop=0, plane=Plane.XY, choice=0)[source]¶
Returns the projection operator for given measurement angle and local Clifford op (VOP).
See also
graphix.clifford- Parameters:
angle (float) – original measurement angle in radian
vop (int) – index of local Clifford (vop), see graphq.clifford.CLIFFORD
plane ('XY', 'YZ' or 'ZX') – measurement plane on which angle shall be defined
choice (0 or 1) – choice of measurement outcome. measured eigenvalue would be (-1)**choice.
- Returns:
op – projection operator
- Return type:
numpy array
- class graphix.sim.statevec.Statevec(data: ~graphix.states.State | ~graphix.sim.statevec.Statevec | ~collections.abc.Iterable[~graphix.states.State] | ~collections.abc.Iterable[~numbers.Number] = PlanarState object defined in plane Plane.XY with angle 0.0., nqubit: int | None = None)[source]¶
Statevector object
- CNOT(qubits)[source]¶
apply CNOT
- Parameters:
qubits (tuple of int) – (control, target) qubit indices
- __init__(data: ~graphix.states.State | ~graphix.sim.statevec.Statevec | ~collections.abc.Iterable[~graphix.states.State] | ~collections.abc.Iterable[~numbers.Number] = PlanarState object defined in plane Plane.XY with angle 0.0., nqubit: int | None = None)[source]¶
Initialize statevector objects. The behaviour is as follows. data can be: - a single
graphix.states.State(classical description of a quantum state) - an iterable ofgraphix.states.Stateobjects - an iterable of scalars (A 2**n numerical statevector) - a graphix.statevec.Statevec objectIf nqubit is not provided, the number of qubit is inferred from data and checked for consistency. If only one
graphix.states.Stateis provided and nqubit is a valid integer, initialize the statevector in the tensor product state. If both nqubit and data are provided, consistency of the dimensions is checked. If a graphix.statevec.Statevec is passed, returns a copy.- Parameters:
data (Data, optional) – input data to prepare the state. Can be a classical description or a numerical input, defaults to graphix.states.BasicStates.PLUS
nqubit (int, optional) – number of qubits to prepare, defaults to None
- entangle(edge: tuple[int, int])[source]¶
connect graph nodes
- Parameters:
edge (tuple of int) – (control, target) qubit indices
- evolve(op: ndarray, qargs: list[int])[source]¶
Multi-qubit operation
- Parameters:
op (numpy.ndarray) – 2^n*2^n matrix
qargs (list of int) – target qubits’ indices
- evolve_single(op: ndarray[Any, dtype[ScalarType]], i: int)[source]¶
Single-qubit operation
- Parameters:
op (numpy.ndarray) – 2*2 matrix
i (int) – qubit index
- expectation_single(op, loc)[source]¶
Expectation value of single-qubit operator.
- Parameters:
op (numpy.ndarray) – 2*2 operator
loc (int) – target qubit index
- Returns:
complex
- Return type:
expectation value.
- expectation_value(op, qargs)[source]¶
Expectation value of multi-qubit operator.
- Parameters:
op (numpy.ndarray) – 2^n*2^n operator
qargs (list of int) – target qubit indices
- Returns:
complex
- Return type:
expectation value
- ptrace(qargs)[source]¶
Perform partial trace of the selected qubits.
Warning
This method currently assumes qubits in qargs to be separable from the rest (checks not implemented for speed). Otherwise, the state returned will be forced to be pure which will result in incorrect output. Correct behaviour will be implemented as soon as the densitymatrix class, currently under development (PR #64), is merged.
- Parameters:
qargs (list of int) – qubit indices to trace over
- remove_qubit(qarg: int)[source]¶
Remove a separable qubit from the system and assemble a statevector for remaining qubits. This results in the same result as partial trace, if the qubit qarg is separable from the rest.
For a statevector \(\ket{\psi} = \sum c_i \ket{i}\) with sum taken over \(i \in [ 0 \dots 00,\ 0\dots 01,\ \dots,\ 1 \dots 11 ]\), this method returns
\[\begin{split}\begin{align} \ket{\psi}' =& c_{0 \dots 0_{\mathrm{k-1}}0_{\mathrm{k}}0_{\mathrm{k+1}} \dots 00} \ket{0 \dots 0_{\mathrm{k-1}}0_{\mathrm{k+1}} \dots 00} \\ & + c_{0 \dots 0_{\mathrm{k-1}}0_{\mathrm{k}}0_{\mathrm{k+1}} \dots 01} \ket{0 \dots 0_{\mathrm{k-1}}0_{\mathrm{k+1}} \dots 01} \\ & + c_{0 \dots 0_{\mathrm{k-1}}0_{\mathrm{k}}0_{\mathrm{k+1}} \dots 10} \ket{0 \dots 0_{\mathrm{k-1}}0_{\mathrm{k+1}} \dots 10} \\ & + \dots \\ & + c_{1 \dots 1_{\mathrm{k-1}}0_{\mathrm{k}}1_{\mathrm{k+1}} \dots 11} \ket{1 \dots 1_{\mathrm{k-1}}1_{\mathrm{k+1}} \dots 11}, \end{align}\end{split}\](after normalization) for \(k =\) qarg. If the \(k\) th qubit is in \(\ket{1}\) state, above will return zero amplitudes; in such a case the returned state will be the one above with \(0_{\mathrm{k}}\) replaced with \(1_{\mathrm{k}}\) .
Warning
This method assumes the qubit with index qarg to be separable from the rest, and is implemented as a significantly faster alternative for partial trace to be used after single-qubit measurements. Care needs to be taken when using this method. Checks for separability will be implemented soon as an option.
See also
graphix.sim.statevec.Statevec.ptrace()and warning therein.- Parameters:
qarg (int) – qubit index
- swap(qubits)[source]¶
swap qubits
- Parameters:
qubits (tuple of int) – (control, target) qubit indices
- tensor(other)[source]¶
Tensor product state with other qubits. Results in self \(\otimes\) other.
- Parameters:
other (
graphix.sim.statevec.Statevec) – statevector to be tensored with self
Density Matrix¶
- class graphix.sim.density_matrix.DensityMatrixBackend(pattern, max_qubit_num=12, pr_calc=True, input_state: ~graphix.states.State | ~graphix.sim.density_matrix.DensityMatrix | ~graphix.sim.statevec.Statevec | ~collections.abc.Iterable[~graphix.states.State] | ~collections.abc.Iterable[~numbers.Number] | ~collections.abc.Iterable[~collections.abc.Iterable[~numbers.Number]] = PlanarState object defined in plane Plane.XY with angle 0.0.)[source]¶
MBQC simulator with density matrix method.
- __init__(pattern, max_qubit_num=12, pr_calc=True, input_state: ~graphix.states.State | ~graphix.sim.density_matrix.DensityMatrix | ~graphix.sim.statevec.Statevec | ~collections.abc.Iterable[~graphix.states.State] | ~collections.abc.Iterable[~numbers.Number] | ~collections.abc.Iterable[~collections.abc.Iterable[~numbers.Number]] = PlanarState object defined in plane Plane.XY with angle 0.0.)[source]¶
- Parameters:
pattern (
graphix.pattern.Patternobject) – Pattern to be simulated.max_qubit_num (int) – optional argument specifying the maximum number of qubits to be stored in the statevector at a time.
pr_calc (bool) – whether or not to compute the probability distribution before choosing the measurement result. if False, measurements yield results 0/1 with 50% probabilities each.
input_state (same syntax as graphix.statevec.DensityMatrix constructor.)
- add_nodes(nodes, input_state: ~graphix.states.State | ~graphix.sim.density_matrix.DensityMatrix | ~graphix.sim.statevec.Statevec | ~collections.abc.Iterable[~graphix.states.State] | ~collections.abc.Iterable[~numbers.Number] | ~collections.abc.Iterable[~collections.abc.Iterable[~numbers.Number]] = PlanarState object defined in plane Plane.XY with angle 0.0.)[source]¶
add new qubit to the internal density matrix and asign the corresponding node number to list self.node_index.
- Parameters:
nodes (list) – list of node indices
qubit_to_add (DensityMatrix object) – qubit to be added to the graph states
- apply_channel(channel: KrausChannel, qargs)[source]¶
backend version of apply_channel :param qargs: :type qargs: list of ints. Target qubits
- apply_clifford(cmd)[source]¶
backend version of apply_channel :param qargs: :type qargs: list of ints. Target qubits
- correct_byproduct(cmd)[source]¶
Byproduct correction correct for the X or Z byproduct operators, by applying the X or Z gate.
- entangle_nodes(edge)[source]¶
Apply CZ gate to the two connected nodes.
- Parameters:
edge (tuple (int, int)) – a pair of node indices
- class graphix.sim.density_matrix.DensityMatrix(data: ~graphix.states.State | ~graphix.sim.density_matrix.DensityMatrix | ~graphix.sim.statevec.Statevec | ~collections.abc.Iterable[~graphix.states.State] | ~collections.abc.Iterable[~numbers.Number] | ~collections.abc.Iterable[~collections.abc.Iterable[~numbers.Number]] = PlanarState object defined in plane Plane.XY with angle 0.0., nqubit: int | None = None)[source]¶
DensityMatrix object.
- __init__(data: ~graphix.states.State | ~graphix.sim.density_matrix.DensityMatrix | ~graphix.sim.statevec.Statevec | ~collections.abc.Iterable[~graphix.states.State] | ~collections.abc.Iterable[~numbers.Number] | ~collections.abc.Iterable[~collections.abc.Iterable[~numbers.Number]] = PlanarState object defined in plane Plane.XY with angle 0.0., nqubit: int | None = None)[source]¶
Initialize density matrix objects. The behaviour builds on theo ne of graphix.statevec.Statevec. data can be: - a single
graphix.states.State(classical description of a quantum state) - an iterable ofgraphix.states.Stateobjects - an iterable of iterable of scalars (A 2**n x 2**n numerical density matrix) - a graphix.statevec.DensityMatrix object - a graphix.statevec.Statevector objectIf nqubit is not provided, the number of qubit is inferred from data and checked for consistency. If only one
graphix.states.Stateis provided and nqubit is a valid integer, initialize the statevector in the tensor product state. If both nqubit and data are provided, consistency of the dimensions is checked. If a graphix.statevec.Statevec or graphix.statevec.DensityMatrix is passed, returns a copy.- Parameters:
data (graphix.states.State | “DensityMatrix” | Statevec | collections.abc.Iterable[graphix.states.State] |collections.abc.Iterable[numbers.Number] | collections.abc.Iterable[collections.abc.Iterable[numbers.Number]], optional) – input data to prepare the state. Can be a classical description or a numerical input, defaults to graphix.states.BasicStates.PLUS
nqubit (int, optional) – number of qubits to prepare, defaults to None
- apply_channel(channel: KrausChannel, qargs)[source]¶
Applies a channel to a density matrix.
:param : :type : rho: density matrix. :param channel: KrausChannel to be applied to the density matrix :type channel:
graphix.channel.KrausChannelobject :param qargs: :type qargs: target qubit indices- Return type:
nothing
- Raises:
ValueError – If the final density matrix is not normalized after application of the channel. This shouldn’t happen since
graphix.channel.KrausChannelobjects are normalized by construction..... –
- cnot(edge)[source]¶
Apply CNOT gate to density matrix.
- Parameters:
edge ((int, int) or [int, int]) – Edge to apply CNOT gate.
- entangle(edge)[source]¶
connect graph nodes
- Parameters:
edge ((int, int) or [int, int]) – (control, target) qubit indices.
- evolve(op, qargs)[source]¶
Multi-qubit operation
- Parameters:
op (np.array) – 2^n*2^n matrix
qargs (list of ints) – target qubits’ indexes
- evolve_single(op, i)[source]¶
Single-qubit operation.
- Parameters:
op (np.ndarray) – 2*2 matrix.
i (int) – Index of qubit to apply operator.
- expectation_single(op, i)[source]¶
Expectation value of single-qubit operator.
- Parameters:
op (np.array) – 2*2 Hermite operator
loc (int) – Index of qubit on which to apply operator.
- Returns:
expectation value (real for hermitian ops!).
- Return type:
complex
- fidelity(statevec)[source]¶
calculate the fidelity against reference statevector.
- Parameters:
statevec (numpy array) – statevector (flattened numpy array) to compare with
- ptrace(qargs)[source]¶
partial trace
- Parameters:
qargs (list of ints or int) – Indices of qubit to trace out.