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
run()[source]

Perform the simulation.

Returns

the output quantum state, in the representation depending on the backend used.

Return type

state

Simulator backends

Tensor Network

class graphix.sim.tensornet.TensorNetworkBackend(pattern, graph_prep='auto', **kwargs)[source]

Tensor Network Simulator for MBQC

Executes the measurement pattern using TN expression of graph states.

__init__(pattern, graph_prep='auto', **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

  • **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)[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)[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)[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]

graphix.sim.tensornet.gen_str()[source]

Generate dummy string for einsum.

graphix.sim.tensornet.proj_basis(angle, vop, plane, choice)[source]

the projected statevector.

Parameters
  • angle (float) – measurement angle

  • vop (int) – CLIFFORD index

  • plane (str) – measurement plane

  • choice (int) – measurement result

Returns

projected state

Return type

numpy.ndarray

graphix.sim.tensornet.outer_product(vectors)[source]

outer product of the given vectors

Parameters

vectors (list of vector) – vectors

Returns

tensor object.

Return type

numpy.ndarray

Statevector

class graphix.sim.statevec.StatevectorBackend(pattern, max_qubit_num=20, pr_calc=True)[source]

MBQC simulator with statevector method.

__init__(pattern, max_qubit_num=20, pr_calc=True)[source]
Parameters
  • pattern (graphix.pattern.Pattern object) – MBQC pattern to be simulated.

  • backend (str, 'statevector') – optional argument for simulation.

  • 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)[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)[source]

Apply single-qubit Clifford gate, specified by vop index specified in graphix.clifford.CLIFFORD

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 two connected nodes

Parameters

edge (tuple (i, j)) – a pair of node indices

finalize()[source]

to be run at the end of pattern simulation.

measure(cmd)[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]

qubit_dim()[source]

Returns the qubit number in the internal statevector

Returns

n_qubit

Return type

int

sort_qubits()[source]

sort the qubit order in internal statevector

graphix.sim.statevec.meas_op(angle, vop=0, 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(nqubit=1, plus_states=True)[source]

Simple statevector simulator

CNOT(qubits)[source]

apply CNOT

Parameters

qubits (tuple of int) – (control, target) qubit indices

__init__(nqubit=1, plus_states=True)[source]

Initialize statevector

Parameters
  • nqubit (int, optional:) – number of qubits. Defaults to 1.

  • plus_states (bool, optional) – whether or not to start all qubits in + state or 0 state. Defaults to +

entangle(edge)[source]

connect graph nodes

Parameters

edge (tuple of int) – (control, target) qubit indices

evolve(op, qargs)[source]

Multi-qubit operation

Parameters
  • op (numpy.ndarray) – 2^n*2^n matrix

  • qargs (list of int) – target qubits’ indices

evolve_single(op, i)[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

flatten()[source]

returns flattened statevector

normalize()[source]

normalize the state

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)[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)[source]

MBQC simulator with density matrix method.

__init__(pattern, max_qubit_num=12, pr_calc=True)[source]
Parameters
  • pattern (graphix.pattern.Pattern object) – 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.

add_nodes(nodes, qubit_to_add=None)[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

finalize()[source]

To be run at the end of pattern simulation.

measure(cmd)[source]

Perform measurement on the specified node and trase out the qubit.

Parameters

cmd (list) – measurement command : [‘M’, node, plane, angle, s_domain, t_domain]

sort_qubits()[source]

sort the qubit order in internal density matrix

class graphix.sim.density_matrix.DensityMatrix(data=None, plus_state=True, nqubit=1)[source]

DensityMatrix object.

__init__(data=None, plus_state=True, nqubit=1)[source]
Parameters
  • data (DensityMatrix, list, tuple, np.ndarray or None) – Density matrix of shape (2**nqubits, 2**nqubits).

  • nqubit (int) – Number of qubits. Default is 1. If both data and nqubit are specified, nqubit is ignored.

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.KrausChannel object :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.KrausChannel objects 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

normalize()[source]

normalize density matrix

ptrace(qargs)[source]

partial trace

Parameters

qargs (list of ints or int) – Indices of qubit to trace out.

swap(edge)[source]

swap qubits

Parameters

edge ((int, int) or [int, int]) – (control, target) qubits indices.

tensor(other)[source]

Tensor product state with other density matrix. Results in self \(\otimes\) other.

Parameters

other – DensityMatrix object to be tensored with self.