Pattern Simulation

graphix.simulator module

class graphix.simulator.PatternSimulator(pattern, backend='statevector', **kwargs)[source]

MBQC simulator

Executes the measurement pattern.

__init__(pattern, backend='statevector', **kwargs)[source]
pattern: graphix.pattern.Pattern object

MBQC pattern to be simulated.

backend: str, ‘statevector’ or ‘MPS’

simulation backend (optional), default is ‘statevector’.

kwargs: keyword args for specified backend.

run()[source]

Perform the simulation.

Returns

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

Return type

state

Simulator backends

Matrix Product State (MPS)

class graphix.sim.mps.MPS(pattern, singular_value=None, max_truncation_err=None, graph_prep='opt')[source]

Matrix Product Simulator for MBQC

Executes the measurement pattern. This is a simple implementation. Improved CPU/GPU MPS simulator will be released soon.

__init__(pattern, singular_value=None, max_truncation_err=None, graph_prep='opt')[source]
Parameters
  • pattern (graphix.Pattern) – MBQC command sequence to be simulated

  • singular_value (int) – cut off threshold for SVD decomposition

  • truncation_err (float) – cut off threshold for SVD decomposition. truncate maximum number of singular values within truncation_err

  • graph_prep (str) – ‘sequential’ for standard method, ‘opt’ for faster method

add_node(n)[source]

Internal method of run_cmd(). Add new qubit to a node set of MPS.

Parameters

(int) (n) –

add_nodes(cmds)[source]

add qubits to node sets from command sequence

apply_clifford(cmd)[source]

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

Parameters

(list) (cmd) –

apply_one_site_operator(loc, node_op)[source]

Internal method for measure, correct_byproduct, and apply_clifford. Apply one site operator to a node.

Parameters
  • (int) (loc) –

  • (tn.Node) (node_op) –

correct_byproduct(cmd)[source]

Perform byproduct correction.

Parameters

(list) (cmd) –

count_maxE()[source]

Count the max number of edges per a node. When maxE is large number, huge memory(2^maxE) will be used.

Returns

int

Return type

The max number of edges.

entangle_nodes(edge)[source]

Make entanglement between nodes specified by edge. Contract non-dangling edges in this process. Optimized contraction will be implemented in a later release.

Parameters

ints) (edge (taple of) –

expectation_value(op, qargs)[source]

calculate expectation value of given operator.

Parameters
  • (numpy.ndarray) (op) –

  • ints) (qargs (list of) –

Returns

expectation_value

Return type

float

expectation_value_ops(ops, qargs)[source]

calculate expectation value of given operators. This command is mainly used for retrieving a probability distribution.

Parameters
  • numpy.ndarray) (ops (list of) – For constructing statevector, ops are projection operators fro each site.

  • ints) (qargs (list of) –

Returns

expectation value

Return type

float

get_amplitude(number)[source]

calculate a probability amplitude of the specified state.

Parameters
  • (int) (number) –

  • 10. (e.g. |0000> corresponds to 0. |1010> corresponds to) –

Returns

float

Return type

the probability amplitude of the specified state.

initialize()[source]

initialize the internal MPS state

make_graph_state()[source]

Prepare a graph state with efficient expression, instead of using CZ gates. This is is an internal method of run_cmd().

See also

make_initial()

make_initial()[source]

This is an internal method of run_cmd. Prepare a graph state sequentially by applying CZ gates.

measure(cmd)[source]

Perform measurement of a node. In MPS, to apply measurement operator to the tensor, consisting Matrix Product State, equals to perform measurement.

Parameters

(list) (cmd) –

replicate_node_dict(node_dict, conjugate=False)[source]

Replicate dictionary of nodes.

Parameters

tensornetwork.Node) (node_dict (dic of) –

Returns

dict of tensornetwork.Node

Return type

replicated node dictionary

set_singular_value(chi)[source]

Set the number of singular values holding under singular value decomposition(SVD).

Note: If you choose ‘opt’ as a graph_prep, you don’t have to specify the singular_value.

Parameters

chi(int) (number of singular values holding when SVD executed) –

set_truncation_err(truncation_err)[source]

Set the max truncation error under SVD.

Note: If you choose ‘opt’ as a graph_prep, you don’t have to specify the truncation_err.

Parameters
  • (float) (truncation_err) –

  • possessed. (Maximum number of singular values keeping truncation error under specified value will be) –

Statevector

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

MBQC simulator with statevector method.

__init__(pattern, max_qubit_num=20)[source]
pattern: graphix.pattern.Pattern object

MBQC pattern to be simulated.

backend: str, ‘statevector’

optional argument for simulation.

max_qubit_numint

optional argument specifying the maximum number of qubits to be stored in the statevector at a time.

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.

initialize()[source]

Initialize the internal statevector

measure(cmd)[source]

Perform measurement of a node in the internal statevector and trace out the qubit :param cmd: measurement command : [‘M’, node, plane angle, s_domain, t_domain] :type cmd: list

qubit_dim()[source]

Returns the qubit number in the internal statevector :returns: n_qubit :rtype: int

sort_qubits()[source]

sort the qubit order in internal statevector

trace_out()[source]

trace out the qubits buffered in self.to_trace from self.state

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(plus_states=True, nqubit=1)[source]

Simple statevector simulator

CNOT(qubits)[source]

apply CNOT

Parameters

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

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

Initialize statevector

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

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

entangle(edge)[source]

connect graph nodes

Parameters

edge (tuple of ints) – (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.array) – 2*2 matrix

  • i (int) – qubit index

expectation_single(op, loc)[source]

Expectation value of single-qubit operator.

Parameters
  • op (np.array) – 2*2 Hermite operator

  • loc (int) – target qubit

Returns

expectation value.

Return type

complex

expectation_value(op, qargs)[source]

Expectation value of multi-qubit operator.

Parameters
  • op (np.array) – 2^n*2^n operator

  • qargs (list of ints) – target qubit

Returns

expectation value

Return type

complex

flatten()[source]

returns flattened statevector

normalize()[source]

normalize the state

ptrace(qargs)[source]

partial trace

Parameters

qargs (list of ints) – qubit inices to trace over

swap(qubits)[source]

swap qubits

Parameters

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

tensor(other)[source]

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

Parameters

other (_type_) – graphix.sim.statevec.Statevec instance