tensorcircuit.densitymatrix#

Quantum circuit class but with density matrix simulator

class tensorcircuit.densitymatrix.DMCircuit(nqubits: int, empty: bool = False, inputs: Optional[Any] = None, mps_inputs: Optional[tensorcircuit.quantum.QuOperator] = None, dminputs: Optional[Any] = None, mpo_dminputs: Optional[tensorcircuit.quantum.QuOperator] = None, split: Optional[Dict[str, Any]] = None)[源代码]#

基类:tensorcircuit.basecircuit.BaseCircuit

ANY(*index: int, **vars: Any) None#

Apply ANY gate with parameters on the circuit. See tensorcircuit.gates.any_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

CNOT(*index: int, **kws: Any) None#

Apply CNOT gate on the circuit. See tensorcircuit.gates.cnot_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j\\ 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j \end{bmatrix}\end{split}\]

CPHASE(*index: int, **vars: Any) None#

Apply CPHASE gate with parameters on the circuit. See tensorcircuit.gates.cphase_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

CR(*index: int, **vars: Any) None#

Apply CR gate with parameters on the circuit. See tensorcircuit.gates.cr_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

CRX(*index: int, **vars: Any) None#

Apply CRX gate with parameters on the circuit. See tensorcircuit.gates.crx_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

CRY(*index: int, **vars: Any) None#

Apply CRY gate with parameters on the circuit. See tensorcircuit.gates.cry_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

CRZ(*index: int, **vars: Any) None#

Apply CRZ gate with parameters on the circuit. See tensorcircuit.gates.crz_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

CU(*index: int, **vars: Any) None#

Apply CU gate with parameters on the circuit. See tensorcircuit.gates.cu_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

CY(*index: int, **kws: Any) None#

Apply CY gate on the circuit. See tensorcircuit.gates.cy_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 0.-1.j\\ 0.+0.j & 0.+0.j & 0.+1.j & 0.+0.j \end{bmatrix}\end{split}\]

CZ(*index: int, **kws: Any) None#

Apply CZ gate on the circuit. See tensorcircuit.gates.cz_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & -1.+0.j \end{bmatrix}\end{split}\]

EXP(*index: int, **vars: Any) None#

Apply EXP gate with parameters on the circuit. See tensorcircuit.gates.exp_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

EXP1(*index: int, **vars: Any) None#

Apply EXP1 gate with parameters on the circuit. See tensorcircuit.gates.exp1_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

FREDKIN(*index: int, **kws: Any) None#

Apply FREDKIN gate on the circuit. See tensorcircuit.gates.fredkin_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j \end{bmatrix}\end{split}\]

H(*index: int, **kws: Any) None#

Apply H gate on the circuit. See tensorcircuit.gates.h_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 0.70710677+0.j & 0.70710677+0.j\\ 0.70710677+0.j & -0.70710677+0.j \end{bmatrix}\end{split}\]

I(*index: int, **kws: Any) None#

Apply I gate on the circuit. See tensorcircuit.gates.i_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 1.+0.j & 0.+0.j\\ 0.+0.j & 1.+0.j \end{bmatrix}\end{split}\]

ISWAP(*index: int, **vars: Any) None#

Apply ISWAP gate with parameters on the circuit. See tensorcircuit.gates.iswap_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

MPO(*index: int, **vars: Any) None#

Apply mpo gate in MPO format on the circuit. See tensorcircuit.gates.mpo_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

MULTICONTROL(*index: int, **vars: Any) None#

Apply multicontrol gate in MPO format on the circuit. See tensorcircuit.gates.multicontrol_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

ORX(*index: int, **vars: Any) None#

Apply ORX gate with parameters on the circuit. See tensorcircuit.gates.orx_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

ORY(*index: int, **vars: Any) None#

Apply ORY gate with parameters on the circuit. See tensorcircuit.gates.ory_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

ORZ(*index: int, **vars: Any) None#

Apply ORZ gate with parameters on the circuit. See tensorcircuit.gates.orz_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

OX(*index: int, **kws: Any) None#

Apply OX gate on the circuit. See tensorcircuit.gates.ox_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j\\ 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j \end{bmatrix}\end{split}\]

OY(*index: int, **kws: Any) None#

Apply OY gate on the circuit. See tensorcircuit.gates.oy_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 0.+0.j & 0.-1.j & 0.+0.j & 0.+0.j\\ 0.+1.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j \end{bmatrix}\end{split}\]

OZ(*index: int, **kws: Any) None#

Apply OZ gate on the circuit. See tensorcircuit.gates.oz_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & -1.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j \end{bmatrix}\end{split}\]

PHASE(*index: int, **vars: Any) None#

Apply PHASE gate with parameters on the circuit. See tensorcircuit.gates.phase_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

R(*index: int, **vars: Any) None#

Apply R gate with parameters on the circuit. See tensorcircuit.gates.r_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

RX(*index: int, **vars: Any) None#

Apply RX gate with parameters on the circuit. See tensorcircuit.gates.rx_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

RXX(*index: int, **vars: Any) None#

Apply RXX gate with parameters on the circuit. See tensorcircuit.gates.rxx_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

RY(*index: int, **vars: Any) None#

Apply RY gate with parameters on the circuit. See tensorcircuit.gates.ry_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

RYY(*index: int, **vars: Any) None#

Apply RYY gate with parameters on the circuit. See tensorcircuit.gates.ryy_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

RZ(*index: int, **vars: Any) None#

Apply RZ gate with parameters on the circuit. See tensorcircuit.gates.rz_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

RZZ(*index: int, **vars: Any) None#

Apply RZZ gate with parameters on the circuit. See tensorcircuit.gates.rzz_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

S(*index: int, **kws: Any) None#

Apply S gate on the circuit. See tensorcircuit.gates.s_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 1.+0.j & 0.+0.j\\ 0.+0.j & 0.+1.j \end{bmatrix}\end{split}\]

SD(*index: int, **kws: Any) None#

Apply SD gate on the circuit. See tensorcircuit.gates.sd_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 1.+0.j & 0.+0.j\\ 0.+0.j & 0.-1.j \end{bmatrix}\end{split}\]

SWAP(*index: int, **kws: Any) None#

Apply SWAP gate on the circuit. See tensorcircuit.gates.swap_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j\\ 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j \end{bmatrix}\end{split}\]

T(*index: int, **kws: Any) None#

Apply T gate on the circuit. See tensorcircuit.gates.t_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 1. & +0.j & 0. & +0.j\\ 0. & +0.j & 0.70710677+0.70710677j \end{bmatrix}\end{split}\]

TD(*index: int, **kws: Any) None#

Apply TD gate on the circuit. See tensorcircuit.gates.td_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 1. & +0.j & 0. & +0.j\\ 0. & +0.j & 0.70710677-0.70710677j \end{bmatrix}\end{split}\]

TOFFOLI(*index: int, **kws: Any) None#

Apply TOFFOLI gate on the circuit. See tensorcircuit.gates.toffoli_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j \end{bmatrix}\end{split}\]

U(*index: int, **vars: Any) None#

Apply U gate with parameters on the circuit. See tensorcircuit.gates.u_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

WROOT(*index: int, **kws: Any) None#

Apply WROOT gate on the circuit. See tensorcircuit.gates.wroot_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 0.70710677+0.j & -0.5 & -0.5j\\ 0.5 & -0.5j & 0.70710677+0.j \end{bmatrix}\end{split}\]

X(*index: int, **kws: Any) None#

Apply X gate on the circuit. See tensorcircuit.gates.x_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 0.+0.j & 1.+0.j\\ 1.+0.j & 0.+0.j \end{bmatrix}\end{split}\]

Y(*index: int, **kws: Any) None#

Apply Y gate on the circuit. See tensorcircuit.gates.y_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 0.+0.j & 0.-1.j\\ 0.+1.j & 0.+0.j \end{bmatrix}\end{split}\]

Z(*index: int, **kws: Any) None#

Apply Z gate on the circuit. See tensorcircuit.gates.z_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 1.+0.j & 0.+0.j\\ 0.+0.j & -1.+0.j \end{bmatrix}\end{split}\]

__init__(nqubits: int, empty: bool = False, inputs: Optional[Any] = None, mps_inputs: Optional[tensorcircuit.quantum.QuOperator] = None, dminputs: Optional[Any] = None, mpo_dminputs: Optional[tensorcircuit.quantum.QuOperator] = None, split: Optional[Dict[str, Any]] = None) None[源代码]#

The density matrix simulator based on tensornetwork engine.

参数
  • nqubits (int) -- Number of qubits

  • empty (bool, optional) -- if True, nothing initialized, only for internal use, defaults to False

  • inputs (Optional[Tensor], optional) -- the state input for the circuit, defaults to None

  • mps_inputs (Optional[QuOperator]) -- QuVector for a MPS like initial pure state.

  • dminputs (Optional[Tensor], optional) -- the density matrix input for the circuit, defaults to None

  • mpo_dminputs (Optional[QuOperator]) -- QuOperator for a MPO like initial density matrix.

  • split (Optional[Dict[str, Any]]) -- dict if two qubit gate is ready for split, including parameters for at least one of max_singular_values and max_truncation_err.

static all_zero_nodes(n: int, d: int = 2, prefix: str = 'qb-') List[tensornetwork.network_components.Node]#
amplitude(l: Union[str, Any]) Any#

Returns the amplitude of the circuit given the bitstring l. For state simulator, it computes \(\langle l\vert \psi\rangle\), for density matrix simulator, it computes \(Tr(\rho \vert l\rangle \langle 1\vert)\) Note how these two are different up to a square operation.

Example

>>> c = tc.Circuit(2)
>>> c.X(0)
>>> c.amplitude("10")
array(1.+0.j, dtype=complex64)
>>> c.CNOT(0, 1)
>>> c.amplitude("11")
array(1.+0.j, dtype=complex64)
参数

l (Union[str, Tensor]) -- The bitstring of 0 and 1s.

返回

The amplitude of the circuit.

返回类型

tn.Node.tensor

amplitudedamping(*index: int, **vars: float) None#

Apply amplitudedamping quantum channel on the circuit. See tensorcircuit.channels.amplitudedampingchannel()

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the channel.

any(*index: int, **vars: Any) None#

Apply ANY gate with parameters on the circuit. See tensorcircuit.gates.any_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

append(c: tensorcircuit.abstractcircuit.AbstractCircuit, indices: Optional[List[int]] = None) tensorcircuit.abstractcircuit.AbstractCircuit#

append circuit c before

Example

>>> c1 = tc.Circuit(2)
>>> c1.H(0)
>>> c1.H(1)
>>> c2 = tc.Circuit(2)
>>> c2.cnot(0, 1)
>>> c1.append(c2)
<tensorcircuit.circuit.Circuit object at 0x7f8402968970>
>>> c1.draw()
    ┌───┐
q_0:┤ H ├──■──
    ├───┤┌─┴─┐
q_1:┤ H ├┤ X ├
    └───┘└───┘
参数
  • c (BaseCircuit) -- The other circuit to be appended

  • indices (Optional[List[int]], optional) -- the qubit indices to which c is appended on. Defaults to None, which means plain concatenation.

返回

The composed circuit

返回类型

BaseCircuit

append_from_qir(qir: List[Dict[str, Any]]) None#

Apply the ciurict in form of quantum intermediate representation after the current cirucit.

Example

>>> c = tc.Circuit(3)
>>> c.H(0)
>>> c.to_qir()
[{'gatef': h, 'gate': Gate(...), 'index': (0,), 'name': 'h', 'split': None, 'mpo': False}]
>>> c2 = tc.Circuit(3)
>>> c2.CNOT(0, 1)
>>> c2.to_qir()
[{'gatef': cnot, 'gate': Gate(...), 'index': (0, 1), 'name': 'cnot', 'split': None, 'mpo': False}]
>>> c.append_from_qir(c2.to_qir())
>>> c.to_qir()
[{'gatef': h, 'gate': Gate(...), 'index': (0,), 'name': 'h', 'split': None, 'mpo': False},
 {'gatef': cnot, 'gate': Gate(...), 'index': (0, 1), 'name': 'cnot', 'split': None, 'mpo': False}]
参数

qir (List[Dict[str, Any]]) -- The quantum intermediate representation.

apply(gate: Union[tensorcircuit.gates.Gate, tensorcircuit.quantum.QuOperator], *index: int, name: Optional[str] = None, split: Optional[Dict[str, Any]] = None, mpo: bool = False, ir_dict: Optional[Dict[str, Any]] = None) None#

An implementation of this method should also append gate directionary to self._qir

apply_general_gate(gate: Union[tensorcircuit.gates.Gate, tensorcircuit.quantum.QuOperator], *index: int, name: Optional[str] = None, split: Optional[Dict[str, Any]] = None, mpo: bool = False, ir_dict: Optional[Dict[str, Any]] = None) None#

An implementation of this method should also append gate directionary to self._qir

static apply_general_gate_delayed(gatef: Callable[[], tensorcircuit.gates.Gate], name: Optional[str] = None, mpo: bool = False) Callable[[...], None]#
apply_general_kraus(kraus: Sequence[tensorcircuit.gates.Gate], index: Sequence[Tuple[int, ...]], **kws: Any) None[源代码]#
static apply_general_kraus_delayed(krausf: Callable[[...], Sequence[tensorcircuit.gates.Gate]]) Callable[[...], None][源代码]#
static apply_general_variable_gate_delayed(gatef: Callable[[...], tensorcircuit.gates.Gate], name: Optional[str] = None, mpo: bool = False) Callable[[...], None]#
barrier_instruction(*index: List[int]) None#

add a barrier instruction flag, no effect on numerical simulation

参数

index (List[int]) -- the corresponding qubits

ccnot(*index: int, **kws: Any) None#

Apply TOFFOLI gate on the circuit. See tensorcircuit.gates.toffoli_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j \end{bmatrix}\end{split}\]

ccx(*index: int, **kws: Any) None#

Apply TOFFOLI gate on the circuit. See tensorcircuit.gates.toffoli_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j \end{bmatrix}\end{split}\]

static check_density_matrix(dm: Any) None[源代码]#
static check_kraus(kraus: Sequence[tensorcircuit.gates.Gate]) bool[源代码]#
circuit_param: Dict[str, Any]#
cnot(*index: int, **kws: Any) None#

Apply CNOT gate on the circuit. See tensorcircuit.gates.cnot_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j\\ 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j \end{bmatrix}\end{split}\]

static coloring_copied_nodes(nodes: Sequence[tensornetwork.network_components.Node], nodes0: Sequence[tensornetwork.network_components.Node], is_dagger: bool = True, flag: str = 'inputs') None#
static coloring_nodes(nodes: Sequence[tensornetwork.network_components.Node], is_dagger: bool = False, flag: str = 'inputs') None#
cond_measure(index: int, status: Optional[float] = None) Any#

Measurement on z basis at index qubit based on quantum amplitude (not post-selection). The highlight is that this method can return the measured result as a int Tensor and thus maintained a jittable pipeline.

Example

>>> c = tc.Circuit(2)
>>> c.H(0)
>>> r = c.cond_measurement(0)
>>> c.conditional_gate(r, [tc.gates.i(), tc.gates.x()], 1)
>>> c.expectation([tc.gates.z(), [0]]), c.expectation([tc.gates.z(), [1]])
# two possible outputs: (1, 1) or (-1, -1)

注解

In terms of DMCircuit, this method returns nothing and the density matrix after this method is kept in mixed state without knowing the measuremet resuslts

参数

index (int) -- the qubit for the z-basis measurement

返回

0 or 1 for z measurement on up and down freedom

返回类型

Tensor

cond_measurement(index: int, status: Optional[float] = None) Any#

Measurement on z basis at index qubit based on quantum amplitude (not post-selection). The highlight is that this method can return the measured result as a int Tensor and thus maintained a jittable pipeline.

Example

>>> c = tc.Circuit(2)
>>> c.H(0)
>>> r = c.cond_measurement(0)
>>> c.conditional_gate(r, [tc.gates.i(), tc.gates.x()], 1)
>>> c.expectation([tc.gates.z(), [0]]), c.expectation([tc.gates.z(), [1]])
# two possible outputs: (1, 1) or (-1, -1)

注解

In terms of DMCircuit, this method returns nothing and the density matrix after this method is kept in mixed state without knowing the measuremet resuslts

参数

index (int) -- the qubit for the z-basis measurement

返回

0 or 1 for z measurement on up and down freedom

返回类型

Tensor

conditional_gate(which: Any, kraus: Sequence[tensorcircuit.gates.Gate], *index: int) None#

Apply which-th gate from kraus list, i.e. apply kraus[which]

参数
  • which (Tensor) -- Tensor of shape [] and dtype int

  • kraus (Sequence[Gate]) -- A list of gate in the form of tc.gate or Tensor

  • index (int) -- the qubit lines the gate applied on

copy() tensorcircuit.abstractcircuit.AbstractCircuit#
static copy_nodes(nodes: Sequence[tensornetwork.network_components.Node], dangling: Optional[Sequence[tensornetwork.network_components.Edge]] = None, conj: Optional[bool] = False) Tuple[List[tensornetwork.network_components.Node], List[tensornetwork.network_components.Edge]]#

copy all nodes and dangling edges correspondingly

返回

cphase(*index: int, **vars: Any) None#

Apply CPHASE gate with parameters on the circuit. See tensorcircuit.gates.cphase_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

cr(*index: int, **vars: Any) None#

Apply CR gate with parameters on the circuit. See tensorcircuit.gates.cr_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

crx(*index: int, **vars: Any) None#

Apply CRX gate with parameters on the circuit. See tensorcircuit.gates.crx_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

cry(*index: int, **vars: Any) None#

Apply CRY gate with parameters on the circuit. See tensorcircuit.gates.cry_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

crz(*index: int, **vars: Any) None#

Apply CRZ gate with parameters on the circuit. See tensorcircuit.gates.crz_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

cswap(*index: int, **kws: Any) None#

Apply FREDKIN gate on the circuit. See tensorcircuit.gates.fredkin_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j \end{bmatrix}\end{split}\]

cu(*index: int, **vars: Any) None#

Apply CU gate with parameters on the circuit. See tensorcircuit.gates.cu_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

cx(*index: int, **kws: Any) None#

Apply CNOT gate on the circuit. See tensorcircuit.gates.cnot_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j\\ 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j \end{bmatrix}\end{split}\]

cy(*index: int, **kws: Any) None#

Apply CY gate on the circuit. See tensorcircuit.gates.cy_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 0.-1.j\\ 0.+0.j & 0.+0.j & 0.+1.j & 0.+0.j \end{bmatrix}\end{split}\]

cz(*index: int, **kws: Any) None#

Apply CZ gate on the circuit. See tensorcircuit.gates.cz_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & -1.+0.j \end{bmatrix}\end{split}\]

densitymatrix(check: bool = False, reuse: bool = True) Any[源代码]#

Return the output density matrix of the circuit.

参数
  • check (bool, optional) -- check whether the final return is a legal density matrix, defaults to False

  • reuse (bool, optional) -- whether to reuse previous results, defaults to True

返回

The output densitymatrix in 2D shape tensor form

返回类型

Tensor

depolarizing(*index: int, **vars: float) None#

Apply depolarizing quantum channel on the circuit. See tensorcircuit.channels.depolarizingchannel()

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the channel.

draw(**kws: Any) Any#

Visualise the circuit. This method recevies the keywords as same as qiskit.circuit.QuantumCircuit.draw. More details can be found here: https://qiskit.org/documentation/stubs/qiskit.circuit.QuantumCircuit.draw.html. Interesting kws options include: ``idle_wires``(bool)

Example

>>> c = tc.Circuit(3)
>>> c.H(1)
>>> c.X(2)
>>> c.CNOT(0, 1)
>>> c.draw(output='text')
q_0: ───────■──
     ┌───┐┌─┴─┐
q_1: ┤ H ├┤ X ├
     ├───┤└───┘
q_2: ┤ X ├─────
     └───┘
exp(*index: int, **vars: Any) None#

Apply EXP gate with parameters on the circuit. See tensorcircuit.gates.exp_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

exp1(*index: int, **vars: Any) None#

Apply EXP1 gate with parameters on the circuit. See tensorcircuit.gates.exp1_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

expectation(*ops: Tuple[tensornetwork.network_components.Node, List[int]], reuse: bool = True, noise_conf: Optional[Any] = None, status: Optional[Any] = None, **kws: Any) <property object at 0x7f7e14faaef0>[源代码]#

Compute the expectation of corresponding operators.

参数
  • ops (Tuple[tn.Node, List[int]]) -- Operator and its position on the circuit, eg. (tc.gates.z(), [1, ]), (tc.gates.x(), [2, ]) is for operator \(Z_1X_2\).

  • reuse (bool) -- whether contract the density matrix in advance, defaults to True

  • noise_conf (Optional[NoiseConf], optional) -- Noise Configuration, defaults to None

  • status (Optional[Tensor], optional) -- external randomness given by tensor uniformly from [0, 1], defaults to None, used for noisfy circuit sampling

返回

Tensor with one element

返回类型

Tensor

expectation_before(*ops: Tuple[tensornetwork.network_components.Node, List[int]], reuse: bool = True, **kws: Any) List[tensornetwork.network_components.Node]#

Get the tensor network in the form of a list of nodes for the expectation calculation before the real contraction

参数

reuse (bool, optional) -- _description_, defaults to True

引发

ValueError -- _description_

返回

_description_

返回类型

List[tn.Node]

expectation_ps(x: Optional[Sequence[int]] = None, y: Optional[Sequence[int]] = None, z: Optional[Sequence[int]] = None, ps: Optional[Sequence[int]] = None, reuse: bool = True, noise_conf: Optional[Any] = None, nmc: int = 1000, status: Optional[Any] = None, **kws: Any) Any#

Shortcut for Pauli string expectation. x, y, z list are for X, Y, Z positions

Example

>>> c = tc.Circuit(2)
>>> c.X(0)
>>> c.H(1)
>>> c.expectation_ps(x=[1], z=[0])
array(-0.99999994+0.j, dtype=complex64)
>>> c = tc.Circuit(2)
>>> c.cnot(0, 1)
>>> c.rx(0, theta=0.4)
>>> c.rx(1, theta=0.8)
>>> c.h(0)
>>> c.h(1)
>>> error1 = tc.channels.generaldepolarizingchannel(0.1, 1)
>>> error2 = tc.channels.generaldepolarizingchannel(0.06, 2)
>>> noise_conf = NoiseConf()
>>> noise_conf.add_noise("rx", error1)
>>> noise_conf.add_noise("cnot", [error2], [[0, 1]])
>>> c.expectation_ps(x=[0], noise_conf=noise_conf, nmc=10000)
(0.46274087-3.764033e-09j)
参数
  • x (Optional[Sequence[int]], optional) -- sites to apply X gate, defaults to None

  • y (Optional[Sequence[int]], optional) -- sites to apply Y gate, defaults to None

  • z (Optional[Sequence[int]], optional) -- sites to apply Z gate, defaults to None

  • ps (Optional[Sequence[int]], optional) -- or one can apply a ps structures instead of x, y, z, e.g. [0, 1, 3, 0, 2, 2] for X_1Z_2Y_4Y_5 defaults to None, ps can overwrite x, y and z

  • reuse (bool, optional) -- whether to cache and reuse the wavefunction, defaults to True

  • noise_conf (Optional[NoiseConf], optional) -- Noise Configuration, defaults to None

  • nmc (int, optional) -- repetition time for Monte Carlo sampling for noisfy calculation, defaults to 1000

  • status (Optional[Tensor], optional) -- external randomness given by tensor uniformly from [0, 1], defaults to None, used for noisfy circuit sampling

返回

Expectation value

返回类型

Tensor

fredkin(*index: int, **kws: Any) None#

Apply FREDKIN gate on the circuit. See tensorcircuit.gates.fredkin_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j \end{bmatrix}\end{split}\]

classmethod from_json(jsonstr: str, circuit_params: Optional[Dict[str, Any]] = None) tensorcircuit.abstractcircuit.AbstractCircuit#

load json str as a Circuit

参数
  • jsonstr (str) -- _description_

  • circuit_params (Optional[Dict[str, Any]], optional) -- Extra circuit parameters in the format of __init__, defaults to None

返回

_description_

返回类型

AbstractCircuit

classmethod from_json_file(file: str, circuit_params: Optional[Dict[str, Any]] = None) tensorcircuit.abstractcircuit.AbstractCircuit#

load json file and convert it to a circuit

参数
  • file (str) -- filename

  • circuit_params (Optional[Dict[str, Any]], optional) -- _description_, defaults to None

返回

_description_

返回类型

AbstractCircuit

classmethod from_openqasm(qasmstr: str, circuit_params: Optional[Dict[str, Any]] = None, keep_measure_order: bool = False) tensorcircuit.abstractcircuit.AbstractCircuit#
classmethod from_openqasm_file(file: str, circuit_params: Optional[Dict[str, Any]] = None, keep_measure_order: bool = False) tensorcircuit.abstractcircuit.AbstractCircuit#
classmethod from_qir(qir: List[Dict[str, Any]], circuit_params: Optional[Dict[str, Any]] = None) tensorcircuit.abstractcircuit.AbstractCircuit#

Restore the circuit from the quantum intermediate representation.

Example

>>> c = tc.Circuit(3)
>>> c.H(0)
>>> c.rx(1, theta=tc.array_to_tensor(0.7))
>>> c.exp1(0, 1, unitary=tc.gates._zz_matrix, theta=tc.array_to_tensor(-0.2), split=split)
>>> len(c)
7
>>> c.expectation((tc.gates.z(), [1]))
array(0.764842+0.j, dtype=complex64)
>>> qirs = c.to_qir()
>>>
>>> c = tc.Circuit.from_qir(qirs, circuit_params={"nqubits": 3})
>>> len(c._nodes)
7
>>> c.expectation((tc.gates.z(), [1]))
array(0.764842+0.j, dtype=complex64)
参数
  • qir (List[Dict[str, Any]]) -- The quantum intermediate representation of a circuit.

  • circuit_params (Optional[Dict[str, Any]]) -- Extra circuit parameters.

返回

The circuit have same gates in the qir.

返回类型

Circuit

classmethod from_qiskit(qc: Any, n: Optional[int] = None, inputs: Optional[List[float]] = None, circuit_params: Optional[Dict[str, Any]] = None, binding_params: Optional[Union[Sequence[float], Dict[Any, float]]] = None) tensorcircuit.abstractcircuit.AbstractCircuit#

Import Qiskit QuantumCircuit object as a tc.Circuit object.

Example

>>> from qiskit import QuantumCircuit
>>> qisc = QuantumCircuit(3)
>>> qisc.h(2)
>>> qisc.cswap(1, 2, 0)
>>> qisc.swap(0, 1)
>>> c = tc.Circuit.from_qiskit(qisc)
参数
  • qc (QuantumCircuit in Qiskit) -- Qiskit Circuit object

  • n (int) -- The number of qubits for the circuit

  • inputs (Optional[List[float]], optional) -- possible input wavefunction for tc.Circuit, defaults to None

  • circuit_params (Optional[Dict[str, Any]]) -- kwargs given in Circuit.__init__ construction function, default to None.

  • binding_params (Optional[Union[Sequence[float], Dict[Any, float]]]) -- (variational) parameters for the circuit. Could be either a sequence or dictionary depending on the type of parameters in the Qiskit circuit. For ParameterVectorElement use sequence. For Parameter use dictionary

返回

The same circuit but as tensorcircuit object

返回类型

Circuit

classmethod from_qsim_file(file: str, circuit_params: Optional[Dict[str, Any]] = None) tensorcircuit.abstractcircuit.AbstractCircuit#
static front_from_nodes(nodes: List[tensornetwork.network_components.Node]) List[tensornetwork.network_components.Edge]#
gate_aliases = [['cnot', 'cx'], ['fredkin', 'cswap'], ['toffoli', 'ccnot'], ['toffoli', 'ccx'], ['any', 'unitary'], ['sd', 'sdg'], ['td', 'tdg']]#
gate_count(gate_list: Optional[Union[str, Sequence[str]]] = None) int#

count the gate number of the circuit

Example

>>> c = tc.Circuit(3)
>>> c.h(0)
>>> c.multicontrol(0, 1, 2, ctrl=[0, 1], unitary=tc.gates._x_matrix)
>>> c.toffolli(1, 2, 0)
>>> c.gate_count()
3
>>> c.gate_count(["multicontrol", "toffoli"])
2
参数

gate_list (Optional[Sequence[str]], optional) -- gate name or gate name list to be counted, defaults to None (counting all gates)

返回

the total number of all gates or gates in the gate_list

返回类型

int

gate_count_by_condition(cond_func: Callable[[Dict[str, Any]], bool]) int#

count the number of gates that satisfy certain condition

Example

>>> c = tc.Circuit(3)
>>> c.x(0)
>>> c.h(0)
>>> c.multicontrol(0, 1, 2, ctrl=[0, 1], unitary=tc.gates._x_matrix)
>>> c.gate_count_by_condition(lambda qir: qir["index"] == (0, ))
2
>>> c.gate_count_by_condition(lambda qir: qir["mpo"])
1
参数

cond_func (Callable[[Dict[str, Any]], bool]) -- the condition for counting the gate

返回

the total number of all gates which satisfy the condition

返回类型

int

gate_summary() Dict[str, int]#

return the summary dictionary on gate type - gate count pair

返回

the gate count dict by gate type

返回类型

Dict[str, int]

general_kraus(kraus: Sequence[tensorcircuit.gates.Gate], index: Sequence[Tuple[int, ...]], **kws: Any) None#
generaldepolarizing(*index: int, **vars: float) None#

Apply generaldepolarizing quantum channel on the circuit. See tensorcircuit.channels.generaldepolarizingchannel()

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the channel.

get_dm_as_quoperator() tensorcircuit.quantum.QuOperator[源代码]#

Get the representation of the output state in the form of QuOperator while maintaining the circuit uncomputed

返回

QuOperator representation of the output state from the circuit

返回类型

QuOperator

get_dm_as_quvector() tensorcircuit.quantum.QuVector#

Get the representation of the output state in the form of QuVector while maintaining the circuit uncomputed

返回

QuVector representation of the output state from the circuit

返回类型

QuVector

get_positional_logical_mapping() Dict[int, int]#

Get positional logical mapping dict based on measure instruction. This function is useful when we only measure part of the qubits in the circuit, to process the count result from partial measurement, we must be aware of the mapping, i.e. for each position in the count bitstring, what is the corresponding qubits (logical) defined on the circuit

返回

positional_logical_mapping

返回类型

Dict[int, int]

get_quvector() tensorcircuit.quantum.QuVector#

Get the representation of the output state in the form of QuVector while maintaining the circuit uncomputed

返回

QuVector representation of the output state from the circuit

返回类型

QuVector

h(*index: int, **kws: Any) None#

Apply H gate on the circuit. See tensorcircuit.gates.h_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 0.70710677+0.j & 0.70710677+0.j\\ 0.70710677+0.j & -0.70710677+0.j \end{bmatrix}\end{split}\]

i(*index: int, **kws: Any) None#

Apply I gate on the circuit. See tensorcircuit.gates.i_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 1.+0.j & 0.+0.j\\ 0.+0.j & 1.+0.j \end{bmatrix}\end{split}\]

initial_mapping(logical_physical_mapping: Dict[int, int], n: Optional[int] = None, circuit_params: Optional[Dict[str, Any]] = None) tensorcircuit.abstractcircuit.AbstractCircuit#

generate a new circuit with the qubit mapping given by logical_physical_mapping

参数
  • logical_physical_mapping (Dict[int, int]) -- how to map logical qubits to the physical qubits on the new circuit

  • n (Optional[int], optional) -- number of qubit of the new circuit, can be different from the original one, defaults to None

  • circuit_params (Optional[Dict[str, Any]], optional) -- _description_, defaults to None

返回

_description_

返回类型

AbstractCircuit

inputs: Any#
inverse(circuit_params: Optional[Dict[str, Any]] = None) tensorcircuit.abstractcircuit.AbstractCircuit#

inverse the circuit, return a new inversed circuit

EXAMPLE

>>> c = tc.Circuit(2)
>>> c.H(0)
>>> c.rzz(1, 2, theta=0.8)
>>> c1 = c.inverse()
参数

circuit_params (Optional[Dict[str, Any]], optional) -- keywords dict for initialization the new circuit, defaults to None

返回

the inversed circuit

返回类型

Circuit

is_dm: bool = True#
is_mps: bool = False#
isotropicdepolarizing(*index: int, **vars: float) None#

Apply isotropicdepolarizing quantum channel on the circuit. See tensorcircuit.channels.isotropicdepolarizingchannel()

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the channel.

iswap(*index: int, **vars: Any) None#

Apply ISWAP gate with parameters on the circuit. See tensorcircuit.gates.iswap_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

measure(*index: int, with_prob: bool = False, status: Optional[Any] = None) Tuple[Any, Any]#

Take measurement to the given quantum lines. This method is jittable is and about 100 times faster than unjit version!

参数
  • index (int) -- Measure on which quantum line.

  • with_prob (bool, optional) -- If true, theoretical probability is also returned.

  • status (Optional[Tensor]) -- external randomness, with shape [index], defaults to None

返回

The sample output and probability (optional) of the quantum line.

返回类型

Tuple[Tensor, Tensor]

measure_instruction(*index: int) None#

add a measurement instruction flag, no effect on numerical simulation

参数

index (int) -- the corresponding qubits

measure_jit(*index: int, with_prob: bool = False, status: Optional[Any] = None) Tuple[Any, Any]#

Take measurement to the given quantum lines. This method is jittable is and about 100 times faster than unjit version!

参数
  • index (int) -- Measure on which quantum line.

  • with_prob (bool, optional) -- If true, theoretical probability is also returned.

  • status (Optional[Tensor]) -- external randomness, with shape [index], defaults to None

返回

The sample output and probability (optional) of the quantum line.

返回类型

Tuple[Tensor, Tensor]

mpo(*index: int, **vars: Any) None#

Apply mpo gate in MPO format on the circuit. See tensorcircuit.gates.mpo_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

mpogates = ['multicontrol', 'mpo']#
multicontrol(*index: int, **vars: Any) None#

Apply multicontrol gate in MPO format on the circuit. See tensorcircuit.gates.multicontrol_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

orx(*index: int, **vars: Any) None#

Apply ORX gate with parameters on the circuit. See tensorcircuit.gates.orx_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

ory(*index: int, **vars: Any) None#

Apply ORY gate with parameters on the circuit. See tensorcircuit.gates.ory_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

orz(*index: int, **vars: Any) None#

Apply ORZ gate with parameters on the circuit. See tensorcircuit.gates.orz_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

ox(*index: int, **kws: Any) None#

Apply OX gate on the circuit. See tensorcircuit.gates.ox_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j\\ 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j \end{bmatrix}\end{split}\]

oy(*index: int, **kws: Any) None#

Apply OY gate on the circuit. See tensorcircuit.gates.oy_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 0.+0.j & 0.-1.j & 0.+0.j & 0.+0.j\\ 0.+1.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j \end{bmatrix}\end{split}\]

oz(*index: int, **kws: Any) None#

Apply OZ gate on the circuit. See tensorcircuit.gates.oz_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & -1.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j \end{bmatrix}\end{split}\]

perfect_sampling(status: Optional[Any] = None) Tuple[str, float]#

Sampling bistrings from the circuit output based on quantum amplitudes. Reference: arXiv:1201.3974.

参数

status (Optional[Tensor]) -- external randomness, with shape [nqubits], defaults to None

返回

Sampled bit string and the corresponding theoretical probability.

返回类型

Tuple[str, float]

phase(*index: int, **vars: Any) None#

Apply PHASE gate with parameters on the circuit. See tensorcircuit.gates.phase_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

phasedamping(*index: int, **vars: float) None#

Apply phasedamping quantum channel on the circuit. See tensorcircuit.channels.phasedampingchannel()

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the channel.

prepend(c: tensorcircuit.abstractcircuit.AbstractCircuit) tensorcircuit.abstractcircuit.AbstractCircuit#

prepend circuit c before

参数

c (BaseCircuit) -- The other circuit to be prepended

返回

The composed circuit

返回类型

BaseCircuit

probability() Any#

get the 2^n length probability vector over computational basis

返回

probability vector

返回类型

Tensor

quvector() tensorcircuit.quantum.QuVector#

Get the representation of the output state in the form of QuVector while maintaining the circuit uncomputed

返回

QuVector representation of the output state from the circuit

返回类型

QuVector

r(*index: int, **vars: Any) None#

Apply R gate with parameters on the circuit. See tensorcircuit.gates.r_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

readouterror_bs(readout_error: Optional[Sequence[Any]] = None, p: Optional[Any] = None) Any#

Apply readout error to original probabilities of bit string and return the noisy probabilities.

Example

>>> readout_error = []
>>> readout_error.append([0.9,0.75])  # readout error for qubit 0, [p0|0,p1|1]
>>> readout_error.append([0.4,0.7])   # readout error for qubit 1, [p0|0,p1|1]
参数
  • readout_error (Optional[Sequence[Any]] Tensor, List, Tuple) -- list of readout error for each qubits.

  • p (Optional[Any]) -- probabilities of bit string

返回类型

Tensor

replace_inputs(inputs: Any) None#

Replace the input state with the circuit structure unchanged.

参数

inputs (Tensor) -- Input wavefunction.

reset(*index: int, **vars: float) None#

Apply reset quantum channel on the circuit. See tensorcircuit.channels.resetchannel()

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the channel.

reset_instruction(*index: int) None#

add a reset instruction flag, no effect on numerical simulation

参数

index (int) -- the corresponding qubits

rx(*index: int, **vars: Any) None#

Apply RX gate with parameters on the circuit. See tensorcircuit.gates.rx_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

rxx(*index: int, **vars: Any) None#

Apply RXX gate with parameters on the circuit. See tensorcircuit.gates.rxx_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

ry(*index: int, **vars: Any) None#

Apply RY gate with parameters on the circuit. See tensorcircuit.gates.ry_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

ryy(*index: int, **vars: Any) None#

Apply RYY gate with parameters on the circuit. See tensorcircuit.gates.ryy_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

rz(*index: int, **vars: Any) None#

Apply RZ gate with parameters on the circuit. See tensorcircuit.gates.rz_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

rzz(*index: int, **vars: Any) None#

Apply RZZ gate with parameters on the circuit. See tensorcircuit.gates.rzz_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

s(*index: int, **kws: Any) None#

Apply S gate on the circuit. See tensorcircuit.gates.s_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 1.+0.j & 0.+0.j\\ 0.+0.j & 0.+1.j \end{bmatrix}\end{split}\]

sample(batch: Optional[int] = None, allow_state: bool = False, readout_error: Optional[Sequence[Any]] = None, format: Optional[str] = None, random_generator: Optional[Any] = None, status: Optional[Any] = None) Any#

batched sampling from state or circuit tensor network directly

参数
  • batch (Optional[int], optional) -- number of samples, defaults to None

  • allow_state (bool, optional) -- if true, we sample from the final state if memory allows, True is preferred, defaults to False

  • readout_error (Optional[Sequence[Any]] Tensor, List, Tuple) -- readout_error, defaults to None

  • format (Optional[str]) -- sample format, defaults to None as backward compatibility check the doc in tensorcircuit.quantum.measurement_results()

  • format -- alias for the argument format

  • random_generator (Optional[Any], optional) -- random generator, defaults to None

  • status (Optional[Tensor]) -- external randomness given by tensor uniformly from [0, 1], if set, can overwrite random_generator

返回

List (if batch) of tuple (binary configuration tensor and corresponding probability) if the format is None, and consistent with format when given

返回类型

Any

sample_expectation_ps(x: Optional[Sequence[int]] = None, y: Optional[Sequence[int]] = None, z: Optional[Sequence[int]] = None, shots: Optional[int] = None, random_generator: Optional[Any] = None, status: Optional[Any] = None, readout_error: Optional[Sequence[Any]] = None, noise_conf: Optional[Any] = None, nmc: int = 1000, statusc: Optional[Any] = None, **kws: Any) Any#

Compute the expectation with given Pauli string with measurement shots numbers

Example

>>> c = tc.Circuit(2)
>>> c.H(0)
>>> c.rx(1, theta=np.pi/2)
>>> c.sample_expectation_ps(x=[0], y=[1])
-0.99999976
>>> readout_error = []
>>> readout_error.append([0.9,0.75])
>>> readout_error.append([0.4,0.7])
>>> c.sample_expectation_ps(x=[0], y=[1],readout_error = readout_error)
>>> c = tc.Circuit(2)
>>> c.cnot(0, 1)
>>> c.rx(0, theta=0.4)
>>> c.rx(1, theta=0.8)
>>> c.h(0)
>>> c.h(1)
>>> error1 = tc.channels.generaldepolarizingchannel(0.1, 1)
>>> error2 = tc.channels.generaldepolarizingchannel(0.06, 2)
>>> readout_error = [[0.9, 0.75],[0.4, 0.7]]
>>> noise_conf = NoiseConf()
>>> noise_conf.add_noise("rx", error1)
>>> noise_conf.add_noise("cnot", [error2], [[0, 1]])
>>> noise_conf.add_noise("readout", readout_error)
>>> c.sample_expectation_ps(x=[0], noise_conf=noise_conf, nmc=10000)
0.44766843
参数
  • x (Optional[Sequence[int]], optional) -- index for Pauli X, defaults to None

  • y (Optional[Sequence[int]], optional) -- index for Pauli Y, defaults to None

  • z (Optional[Sequence[int]], optional) -- index for Pauli Z, defaults to None

  • shots (Optional[int], optional) -- number of measurement shots, defaults to None, indicating analytical result

  • random_generator (Optional[Any]) -- random_generator, defaults to None

  • status (Optional[Tensor]) -- external randomness given by tensor uniformly from [0, 1], if set, can overwrite random_generator

  • readout_error (Optional[Sequence[Any]] Tensor, List, Tuple) -- readout_error, defaults to None. Overrided if noise_conf is provided.

  • noise_conf (Optional[NoiseConf], optional) -- Noise Configuration, defaults to None

  • nmc (int, optional) -- repetition time for Monte Carlo sampling for noisfy calculation, defaults to 1000

  • statusc (Optional[Tensor], optional) -- external randomness given by tensor uniformly from [0, 1], defaults to None, used for noisfy circuit sampling

返回

[description]

返回类型

Tensor

sd(*index: int, **kws: Any) None#

Apply SD gate on the circuit. See tensorcircuit.gates.sd_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 1.+0.j & 0.+0.j\\ 0.+0.j & 0.-1.j \end{bmatrix}\end{split}\]

sdg(*index: int, **kws: Any) None#

Apply SD gate on the circuit. See tensorcircuit.gates.sd_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 1.+0.j & 0.+0.j\\ 0.+0.j & 0.-1.j \end{bmatrix}\end{split}\]

select_gate(which: Any, kraus: Sequence[tensorcircuit.gates.Gate], *index: int) None#

Apply which-th gate from kraus list, i.e. apply kraus[which]

参数
  • which (Tensor) -- Tensor of shape [] and dtype int

  • kraus (Sequence[Gate]) -- A list of gate in the form of tc.gate or Tensor

  • index (int) -- the qubit lines the gate applied on

sexpps(x: Optional[Sequence[int]] = None, y: Optional[Sequence[int]] = None, z: Optional[Sequence[int]] = None, shots: Optional[int] = None, random_generator: Optional[Any] = None, status: Optional[Any] = None, readout_error: Optional[Sequence[Any]] = None, noise_conf: Optional[Any] = None, nmc: int = 1000, statusc: Optional[Any] = None, **kws: Any) Any#

Compute the expectation with given Pauli string with measurement shots numbers

Example

>>> c = tc.Circuit(2)
>>> c.H(0)
>>> c.rx(1, theta=np.pi/2)
>>> c.sample_expectation_ps(x=[0], y=[1])
-0.99999976
>>> readout_error = []
>>> readout_error.append([0.9,0.75])
>>> readout_error.append([0.4,0.7])
>>> c.sample_expectation_ps(x=[0], y=[1],readout_error = readout_error)
>>> c = tc.Circuit(2)
>>> c.cnot(0, 1)
>>> c.rx(0, theta=0.4)
>>> c.rx(1, theta=0.8)
>>> c.h(0)
>>> c.h(1)
>>> error1 = tc.channels.generaldepolarizingchannel(0.1, 1)
>>> error2 = tc.channels.generaldepolarizingchannel(0.06, 2)
>>> readout_error = [[0.9, 0.75],[0.4, 0.7]]
>>> noise_conf = NoiseConf()
>>> noise_conf.add_noise("rx", error1)
>>> noise_conf.add_noise("cnot", [error2], [[0, 1]])
>>> noise_conf.add_noise("readout", readout_error)
>>> c.sample_expectation_ps(x=[0], noise_conf=noise_conf, nmc=10000)
0.44766843
参数
  • x (Optional[Sequence[int]], optional) -- index for Pauli X, defaults to None

  • y (Optional[Sequence[int]], optional) -- index for Pauli Y, defaults to None

  • z (Optional[Sequence[int]], optional) -- index for Pauli Z, defaults to None

  • shots (Optional[int], optional) -- number of measurement shots, defaults to None, indicating analytical result

  • random_generator (Optional[Any]) -- random_generator, defaults to None

  • status (Optional[Tensor]) -- external randomness given by tensor uniformly from [0, 1], if set, can overwrite random_generator

  • readout_error (Optional[Sequence[Any]] Tensor, List, Tuple) -- readout_error, defaults to None. Overrided if noise_conf is provided.

  • noise_conf (Optional[NoiseConf], optional) -- Noise Configuration, defaults to None

  • nmc (int, optional) -- repetition time for Monte Carlo sampling for noisfy calculation, defaults to 1000

  • statusc (Optional[Tensor], optional) -- external randomness given by tensor uniformly from [0, 1], defaults to None, used for noisfy circuit sampling

返回

[description]

返回类型

Tensor

sgates = ['i', 'x', 'y', 'z', 'h', 't', 's', 'td', 'sd', 'wroot', 'cnot', 'cz', 'swap', 'cy', 'ox', 'oy', 'oz', 'toffoli', 'fredkin']#
split: Optional[Dict[str, Any]]#
static standardize_gate(name: str) str#

standardize the gate name to tc common gate sets

参数

name (str) -- non-standard gate name

返回

the standard gate name

返回类型

str

state(check: bool = False, reuse: bool = True) Any#

Return the output density matrix of the circuit.

参数
  • check (bool, optional) -- check whether the final return is a legal density matrix, defaults to False

  • reuse (bool, optional) -- whether to reuse previous results, defaults to True

返回

The output densitymatrix in 2D shape tensor form

返回类型

Tensor

swap(*index: int, **kws: Any) None#

Apply SWAP gate on the circuit. See tensorcircuit.gates.swap_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j\\ 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j \end{bmatrix}\end{split}\]

t(*index: int, **kws: Any) None#

Apply T gate on the circuit. See tensorcircuit.gates.t_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 1. & +0.j & 0. & +0.j\\ 0. & +0.j & 0.70710677+0.70710677j \end{bmatrix}\end{split}\]

td(*index: int, **kws: Any) None#

Apply TD gate on the circuit. See tensorcircuit.gates.td_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 1. & +0.j & 0. & +0.j\\ 0. & +0.j & 0.70710677-0.70710677j \end{bmatrix}\end{split}\]

tdg(*index: int, **kws: Any) None#

Apply TD gate on the circuit. See tensorcircuit.gates.td_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 1. & +0.j & 0. & +0.j\\ 0. & +0.j & 0.70710677-0.70710677j \end{bmatrix}\end{split}\]

tex(**kws: Any) str#

Generate latex string based on quantikz latex package

返回

Latex string that can be directly compiled via, e.g. latexit

返回类型

str

thermalrelaxation(*index: int, **vars: float) None#

Apply thermalrelaxation quantum channel on the circuit. See tensorcircuit.channels.thermalrelaxationchannel()

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the channel.

to_circuit(circuit_params: Optional[Dict[str, Any]] = None) tensorcircuit.circuit.Circuit[源代码]#

convert into state simulator (current implementation ignores all noise channels)

参数

circuit_params (Optional[Dict[str, Any]], optional) -- kws to initialize circuit object, defaults to None

返回

Circuit with no noise

返回类型

Circuit

to_cirq(enable_instruction: bool = False) Any#

Translate tc.Circuit to a cirq circuit object.

参数

enable_instruction (bool, defaults to False) -- whether also export measurement and reset instructions

返回

A cirq circuit of this circuit.

to_graphviz(graph: Optional[graphviz.graphs.Graph] = None, include_all_names: bool = False, engine: str = 'neato') graphviz.graphs.Graph#

Not an ideal visualization for quantum circuit, but reserve here as a general approach to show the tensornetwork [Deprecated, use Circuit.vis_tex or Circuit.draw instead]

to_json(file: Optional[str] = None, simplified: bool = False) Any#

circuit dumps to json

参数
  • file (Optional[str], optional) -- file str to dump the json to, defaults to None, return the json str

  • simplified (bool) -- If False, keep all info for each gate, defaults to be False. If True, suitable for IO since less information is required

返回

None if dumps to file otherwise the json str

返回类型

Any

to_openqasm(**kws: Any) str#

transform circuit to openqasm via qiskit circuit, see https://qiskit.org/documentation/stubs/qiskit.circuit.QuantumCircuit.qasm.html for usage on possible options for kws

返回

circuit representation in openqasm format

返回类型

str

to_qir() List[Dict[str, Any]]#

Return the quantum intermediate representation of the circuit.

Example

>>> c = tc.Circuit(2)
>>> c.CNOT(0, 1)
>>> c.to_qir()
[{'gatef': cnot, 'gate': Gate(
    name: 'cnot',
    tensor:
        array([[[[1.+0.j, 0.+0.j],
                [0.+0.j, 0.+0.j]],

                [[0.+0.j, 1.+0.j],
                [0.+0.j, 0.+0.j]]],


            [[[0.+0.j, 0.+0.j],
                [0.+0.j, 1.+0.j]],

                [[0.+0.j, 0.+0.j],
                [1.+0.j, 0.+0.j]]]], dtype=complex64),
    edges: [
        Edge(Dangling Edge)[0],
        Edge(Dangling Edge)[1],
        Edge('cnot'[2] -> 'qb-1'[0] ),
        Edge('cnot'[3] -> 'qb-2'[0] )
    ]), 'index': (0, 1), 'name': 'cnot', 'split': None, 'mpo': False}]
返回

The quantum intermediate representation of the circuit.

返回类型

List[Dict[str, Any]]

to_qiskit(enable_instruction: bool = False, enable_inputs: bool = False) Any#

Translate tc.Circuit to a qiskit QuantumCircuit object.

参数
  • enable_instruction (bool, defaults to False) -- whether also export measurement and reset instructions

  • enable_inputs (bool, defaults to False) -- whether also export the inputs

返回

A qiskit object of this circuit.

toffoli(*index: int, **kws: Any) None#

Apply TOFFOLI gate on the circuit. See tensorcircuit.gates.toffoli_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j \end{bmatrix}\end{split}\]

u(*index: int, **vars: Any) None#

Apply U gate with parameters on the circuit. See tensorcircuit.gates.u_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

unitary(*index: int, **vars: Any) None#

Apply ANY gate with parameters on the circuit. See tensorcircuit.gates.any_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

vgates = ['r', 'cr', 'u', 'cu', 'rx', 'ry', 'rz', 'phase', 'rxx', 'ryy', 'rzz', 'cphase', 'crx', 'cry', 'crz', 'orx', 'ory', 'orz', 'iswap', 'any', 'exp', 'exp1']#
vis_tex(**kws: Any) str#

Generate latex string based on quantikz latex package

返回

Latex string that can be directly compiled via, e.g. latexit

返回类型

str

wavefunction() Any[源代码]#

get the wavefunction of outputs, raise error if the final state is not purified [Experimental: the phase factor is not fixed for different backend]

返回

wavefunction vector

返回类型

Tensor

wroot(*index: int, **kws: Any) None#

Apply WROOT gate on the circuit. See tensorcircuit.gates.wroot_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 0.70710677+0.j & -0.5 & -0.5j\\ 0.5 & -0.5j & 0.70710677+0.j \end{bmatrix}\end{split}\]

x(*index: int, **kws: Any) None#

Apply X gate on the circuit. See tensorcircuit.gates.x_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 0.+0.j & 1.+0.j\\ 1.+0.j & 0.+0.j \end{bmatrix}\end{split}\]

y(*index: int, **kws: Any) None#

Apply Y gate on the circuit. See tensorcircuit.gates.y_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 0.+0.j & 0.-1.j\\ 0.+1.j & 0.+0.j \end{bmatrix}\end{split}\]

z(*index: int, **kws: Any) None#

Apply Z gate on the circuit. See tensorcircuit.gates.z_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 1.+0.j & 0.+0.j\\ 0.+0.j & -1.+0.j \end{bmatrix}\end{split}\]

class tensorcircuit.densitymatrix.DMCircuit2(nqubits: int, empty: bool = False, inputs: Optional[Any] = None, mps_inputs: Optional[tensorcircuit.quantum.QuOperator] = None, dminputs: Optional[Any] = None, mpo_dminputs: Optional[tensorcircuit.quantum.QuOperator] = None, split: Optional[Dict[str, Any]] = None)[源代码]#

基类:tensorcircuit.densitymatrix.DMCircuit

ANY(*index: int, **vars: Any) None#

Apply ANY gate with parameters on the circuit. See tensorcircuit.gates.any_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

CNOT(*index: int, **kws: Any) None#

Apply CNOT gate on the circuit. See tensorcircuit.gates.cnot_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j\\ 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j \end{bmatrix}\end{split}\]

CPHASE(*index: int, **vars: Any) None#

Apply CPHASE gate with parameters on the circuit. See tensorcircuit.gates.cphase_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

CR(*index: int, **vars: Any) None#

Apply CR gate with parameters on the circuit. See tensorcircuit.gates.cr_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

CRX(*index: int, **vars: Any) None#

Apply CRX gate with parameters on the circuit. See tensorcircuit.gates.crx_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

CRY(*index: int, **vars: Any) None#

Apply CRY gate with parameters on the circuit. See tensorcircuit.gates.cry_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

CRZ(*index: int, **vars: Any) None#

Apply CRZ gate with parameters on the circuit. See tensorcircuit.gates.crz_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

CU(*index: int, **vars: Any) None#

Apply CU gate with parameters on the circuit. See tensorcircuit.gates.cu_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

CY(*index: int, **kws: Any) None#

Apply CY gate on the circuit. See tensorcircuit.gates.cy_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 0.-1.j\\ 0.+0.j & 0.+0.j & 0.+1.j & 0.+0.j \end{bmatrix}\end{split}\]

CZ(*index: int, **kws: Any) None#

Apply CZ gate on the circuit. See tensorcircuit.gates.cz_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & -1.+0.j \end{bmatrix}\end{split}\]

EXP(*index: int, **vars: Any) None#

Apply EXP gate with parameters on the circuit. See tensorcircuit.gates.exp_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

EXP1(*index: int, **vars: Any) None#

Apply EXP1 gate with parameters on the circuit. See tensorcircuit.gates.exp1_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

FREDKIN(*index: int, **kws: Any) None#

Apply FREDKIN gate on the circuit. See tensorcircuit.gates.fredkin_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j \end{bmatrix}\end{split}\]

H(*index: int, **kws: Any) None#

Apply H gate on the circuit. See tensorcircuit.gates.h_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 0.70710677+0.j & 0.70710677+0.j\\ 0.70710677+0.j & -0.70710677+0.j \end{bmatrix}\end{split}\]

I(*index: int, **kws: Any) None#

Apply I gate on the circuit. See tensorcircuit.gates.i_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 1.+0.j & 0.+0.j\\ 0.+0.j & 1.+0.j \end{bmatrix}\end{split}\]

ISWAP(*index: int, **vars: Any) None#

Apply ISWAP gate with parameters on the circuit. See tensorcircuit.gates.iswap_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

MPO(*index: int, **vars: Any) None#

Apply mpo gate in MPO format on the circuit. See tensorcircuit.gates.mpo_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

MULTICONTROL(*index: int, **vars: Any) None#

Apply multicontrol gate in MPO format on the circuit. See tensorcircuit.gates.multicontrol_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

ORX(*index: int, **vars: Any) None#

Apply ORX gate with parameters on the circuit. See tensorcircuit.gates.orx_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

ORY(*index: int, **vars: Any) None#

Apply ORY gate with parameters on the circuit. See tensorcircuit.gates.ory_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

ORZ(*index: int, **vars: Any) None#

Apply ORZ gate with parameters on the circuit. See tensorcircuit.gates.orz_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

OX(*index: int, **kws: Any) None#

Apply OX gate on the circuit. See tensorcircuit.gates.ox_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j\\ 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j \end{bmatrix}\end{split}\]

OY(*index: int, **kws: Any) None#

Apply OY gate on the circuit. See tensorcircuit.gates.oy_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 0.+0.j & 0.-1.j & 0.+0.j & 0.+0.j\\ 0.+1.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j \end{bmatrix}\end{split}\]

OZ(*index: int, **kws: Any) None#

Apply OZ gate on the circuit. See tensorcircuit.gates.oz_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & -1.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j \end{bmatrix}\end{split}\]

PHASE(*index: int, **vars: Any) None#

Apply PHASE gate with parameters on the circuit. See tensorcircuit.gates.phase_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

R(*index: int, **vars: Any) None#

Apply R gate with parameters on the circuit. See tensorcircuit.gates.r_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

RX(*index: int, **vars: Any) None#

Apply RX gate with parameters on the circuit. See tensorcircuit.gates.rx_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

RXX(*index: int, **vars: Any) None#

Apply RXX gate with parameters on the circuit. See tensorcircuit.gates.rxx_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

RY(*index: int, **vars: Any) None#

Apply RY gate with parameters on the circuit. See tensorcircuit.gates.ry_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

RYY(*index: int, **vars: Any) None#

Apply RYY gate with parameters on the circuit. See tensorcircuit.gates.ryy_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

RZ(*index: int, **vars: Any) None#

Apply RZ gate with parameters on the circuit. See tensorcircuit.gates.rz_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

RZZ(*index: int, **vars: Any) None#

Apply RZZ gate with parameters on the circuit. See tensorcircuit.gates.rzz_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

S(*index: int, **kws: Any) None#

Apply S gate on the circuit. See tensorcircuit.gates.s_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 1.+0.j & 0.+0.j\\ 0.+0.j & 0.+1.j \end{bmatrix}\end{split}\]

SD(*index: int, **kws: Any) None#

Apply SD gate on the circuit. See tensorcircuit.gates.sd_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 1.+0.j & 0.+0.j\\ 0.+0.j & 0.-1.j \end{bmatrix}\end{split}\]

SWAP(*index: int, **kws: Any) None#

Apply SWAP gate on the circuit. See tensorcircuit.gates.swap_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j\\ 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j \end{bmatrix}\end{split}\]

T(*index: int, **kws: Any) None#

Apply T gate on the circuit. See tensorcircuit.gates.t_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 1. & +0.j & 0. & +0.j\\ 0. & +0.j & 0.70710677+0.70710677j \end{bmatrix}\end{split}\]

TD(*index: int, **kws: Any) None#

Apply TD gate on the circuit. See tensorcircuit.gates.td_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 1. & +0.j & 0. & +0.j\\ 0. & +0.j & 0.70710677-0.70710677j \end{bmatrix}\end{split}\]

TOFFOLI(*index: int, **kws: Any) None#

Apply TOFFOLI gate on the circuit. See tensorcircuit.gates.toffoli_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j \end{bmatrix}\end{split}\]

U(*index: int, **vars: Any) None#

Apply U gate with parameters on the circuit. See tensorcircuit.gates.u_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

WROOT(*index: int, **kws: Any) None#

Apply WROOT gate on the circuit. See tensorcircuit.gates.wroot_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 0.70710677+0.j & -0.5 & -0.5j\\ 0.5 & -0.5j & 0.70710677+0.j \end{bmatrix}\end{split}\]

X(*index: int, **kws: Any) None#

Apply X gate on the circuit. See tensorcircuit.gates.x_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 0.+0.j & 1.+0.j\\ 1.+0.j & 0.+0.j \end{bmatrix}\end{split}\]

Y(*index: int, **kws: Any) None#

Apply Y gate on the circuit. See tensorcircuit.gates.y_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 0.+0.j & 0.-1.j\\ 0.+1.j & 0.+0.j \end{bmatrix}\end{split}\]

Z(*index: int, **kws: Any) None#

Apply Z gate on the circuit. See tensorcircuit.gates.z_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 1.+0.j & 0.+0.j\\ 0.+0.j & -1.+0.j \end{bmatrix}\end{split}\]

__init__(nqubits: int, empty: bool = False, inputs: Optional[Any] = None, mps_inputs: Optional[tensorcircuit.quantum.QuOperator] = None, dminputs: Optional[Any] = None, mpo_dminputs: Optional[tensorcircuit.quantum.QuOperator] = None, split: Optional[Dict[str, Any]] = None) None#

The density matrix simulator based on tensornetwork engine.

参数
  • nqubits (int) -- Number of qubits

  • empty (bool, optional) -- if True, nothing initialized, only for internal use, defaults to False

  • inputs (Optional[Tensor], optional) -- the state input for the circuit, defaults to None

  • mps_inputs (Optional[QuOperator]) -- QuVector for a MPS like initial pure state.

  • dminputs (Optional[Tensor], optional) -- the density matrix input for the circuit, defaults to None

  • mpo_dminputs (Optional[QuOperator]) -- QuOperator for a MPO like initial density matrix.

  • split (Optional[Dict[str, Any]]) -- dict if two qubit gate is ready for split, including parameters for at least one of max_singular_values and max_truncation_err.

static all_zero_nodes(n: int, d: int = 2, prefix: str = 'qb-') List[tensornetwork.network_components.Node]#
amplitude(l: Union[str, Any]) Any#

Returns the amplitude of the circuit given the bitstring l. For state simulator, it computes \(\langle l\vert \psi\rangle\), for density matrix simulator, it computes \(Tr(\rho \vert l\rangle \langle 1\vert)\) Note how these two are different up to a square operation.

Example

>>> c = tc.Circuit(2)
>>> c.X(0)
>>> c.amplitude("10")
array(1.+0.j, dtype=complex64)
>>> c.CNOT(0, 1)
>>> c.amplitude("11")
array(1.+0.j, dtype=complex64)
参数

l (Union[str, Tensor]) -- The bitstring of 0 and 1s.

返回

The amplitude of the circuit.

返回类型

tn.Node.tensor

amplitudedamping(*index: int, **vars: float) None#

Apply amplitudedamping quantum channel on the circuit. See tensorcircuit.channels.amplitudedampingchannel()

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the channel.

any(*index: int, **vars: Any) None#

Apply ANY gate with parameters on the circuit. See tensorcircuit.gates.any_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

append(c: tensorcircuit.abstractcircuit.AbstractCircuit, indices: Optional[List[int]] = None) tensorcircuit.abstractcircuit.AbstractCircuit#

append circuit c before

Example

>>> c1 = tc.Circuit(2)
>>> c1.H(0)
>>> c1.H(1)
>>> c2 = tc.Circuit(2)
>>> c2.cnot(0, 1)
>>> c1.append(c2)
<tensorcircuit.circuit.Circuit object at 0x7f8402968970>
>>> c1.draw()
    ┌───┐
q_0:┤ H ├──■──
    ├───┤┌─┴─┐
q_1:┤ H ├┤ X ├
    └───┘└───┘
参数
  • c (BaseCircuit) -- The other circuit to be appended

  • indices (Optional[List[int]], optional) -- the qubit indices to which c is appended on. Defaults to None, which means plain concatenation.

返回

The composed circuit

返回类型

BaseCircuit

append_from_qir(qir: List[Dict[str, Any]]) None#

Apply the ciurict in form of quantum intermediate representation after the current cirucit.

Example

>>> c = tc.Circuit(3)
>>> c.H(0)
>>> c.to_qir()
[{'gatef': h, 'gate': Gate(...), 'index': (0,), 'name': 'h', 'split': None, 'mpo': False}]
>>> c2 = tc.Circuit(3)
>>> c2.CNOT(0, 1)
>>> c2.to_qir()
[{'gatef': cnot, 'gate': Gate(...), 'index': (0, 1), 'name': 'cnot', 'split': None, 'mpo': False}]
>>> c.append_from_qir(c2.to_qir())
>>> c.to_qir()
[{'gatef': h, 'gate': Gate(...), 'index': (0,), 'name': 'h', 'split': None, 'mpo': False},
 {'gatef': cnot, 'gate': Gate(...), 'index': (0, 1), 'name': 'cnot', 'split': None, 'mpo': False}]
参数

qir (List[Dict[str, Any]]) -- The quantum intermediate representation.

apply(gate: Union[tensorcircuit.gates.Gate, tensorcircuit.quantum.QuOperator], *index: int, name: Optional[str] = None, split: Optional[Dict[str, Any]] = None, mpo: bool = False, ir_dict: Optional[Dict[str, Any]] = None) None#

An implementation of this method should also append gate directionary to self._qir

apply_general_gate(gate: Union[tensorcircuit.gates.Gate, tensorcircuit.quantum.QuOperator], *index: int, name: Optional[str] = None, split: Optional[Dict[str, Any]] = None, mpo: bool = False, ir_dict: Optional[Dict[str, Any]] = None) None#

An implementation of this method should also append gate directionary to self._qir

static apply_general_gate_delayed(gatef: Callable[[], tensorcircuit.gates.Gate], name: Optional[str] = None, mpo: bool = False) Callable[[...], None]#
apply_general_kraus(kraus: Sequence[tensorcircuit.gates.Gate], *index: int, **kws: Any) None[源代码]#
static apply_general_kraus_delayed(krausf: Callable[[...], Sequence[tensorcircuit.gates.Gate]]) Callable[[...], None][源代码]#
static apply_general_variable_gate_delayed(gatef: Callable[[...], tensorcircuit.gates.Gate], name: Optional[str] = None, mpo: bool = False) Callable[[...], None]#
barrier_instruction(*index: List[int]) None#

add a barrier instruction flag, no effect on numerical simulation

参数

index (List[int]) -- the corresponding qubits

ccnot(*index: int, **kws: Any) None#

Apply TOFFOLI gate on the circuit. See tensorcircuit.gates.toffoli_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j \end{bmatrix}\end{split}\]

ccx(*index: int, **kws: Any) None#

Apply TOFFOLI gate on the circuit. See tensorcircuit.gates.toffoli_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j \end{bmatrix}\end{split}\]

static check_density_matrix(dm: Any) None#
static check_kraus(kraus: Sequence[tensorcircuit.gates.Gate]) bool#
circuit_param: Dict[str, Any]#
cnot(*index: int, **kws: Any) None#

Apply CNOT gate on the circuit. See tensorcircuit.gates.cnot_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j\\ 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j \end{bmatrix}\end{split}\]

static coloring_copied_nodes(nodes: Sequence[tensornetwork.network_components.Node], nodes0: Sequence[tensornetwork.network_components.Node], is_dagger: bool = True, flag: str = 'inputs') None#
static coloring_nodes(nodes: Sequence[tensornetwork.network_components.Node], is_dagger: bool = False, flag: str = 'inputs') None#
cond_measure(index: int, status: Optional[float] = None) Any#

Measurement on z basis at index qubit based on quantum amplitude (not post-selection). The highlight is that this method can return the measured result as a int Tensor and thus maintained a jittable pipeline.

Example

>>> c = tc.Circuit(2)
>>> c.H(0)
>>> r = c.cond_measurement(0)
>>> c.conditional_gate(r, [tc.gates.i(), tc.gates.x()], 1)
>>> c.expectation([tc.gates.z(), [0]]), c.expectation([tc.gates.z(), [1]])
# two possible outputs: (1, 1) or (-1, -1)

注解

In terms of DMCircuit, this method returns nothing and the density matrix after this method is kept in mixed state without knowing the measuremet resuslts

参数

index (int) -- the qubit for the z-basis measurement

返回

0 or 1 for z measurement on up and down freedom

返回类型

Tensor

cond_measurement(index: int, status: Optional[float] = None) Any#

Measurement on z basis at index qubit based on quantum amplitude (not post-selection). The highlight is that this method can return the measured result as a int Tensor and thus maintained a jittable pipeline.

Example

>>> c = tc.Circuit(2)
>>> c.H(0)
>>> r = c.cond_measurement(0)
>>> c.conditional_gate(r, [tc.gates.i(), tc.gates.x()], 1)
>>> c.expectation([tc.gates.z(), [0]]), c.expectation([tc.gates.z(), [1]])
# two possible outputs: (1, 1) or (-1, -1)

注解

In terms of DMCircuit, this method returns nothing and the density matrix after this method is kept in mixed state without knowing the measuremet resuslts

参数

index (int) -- the qubit for the z-basis measurement

返回

0 or 1 for z measurement on up and down freedom

返回类型

Tensor

conditional_gate(which: Any, kraus: Sequence[tensorcircuit.gates.Gate], *index: int) None#

Apply which-th gate from kraus list, i.e. apply kraus[which]

参数
  • which (Tensor) -- Tensor of shape [] and dtype int

  • kraus (Sequence[Gate]) -- A list of gate in the form of tc.gate or Tensor

  • index (int) -- the qubit lines the gate applied on

copy() tensorcircuit.abstractcircuit.AbstractCircuit#
static copy_nodes(nodes: Sequence[tensornetwork.network_components.Node], dangling: Optional[Sequence[tensornetwork.network_components.Edge]] = None, conj: Optional[bool] = False) Tuple[List[tensornetwork.network_components.Node], List[tensornetwork.network_components.Edge]]#

copy all nodes and dangling edges correspondingly

返回

cphase(*index: int, **vars: Any) None#

Apply CPHASE gate with parameters on the circuit. See tensorcircuit.gates.cphase_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

cr(*index: int, **vars: Any) None#

Apply CR gate with parameters on the circuit. See tensorcircuit.gates.cr_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

crx(*index: int, **vars: Any) None#

Apply CRX gate with parameters on the circuit. See tensorcircuit.gates.crx_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

cry(*index: int, **vars: Any) None#

Apply CRY gate with parameters on the circuit. See tensorcircuit.gates.cry_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

crz(*index: int, **vars: Any) None#

Apply CRZ gate with parameters on the circuit. See tensorcircuit.gates.crz_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

cswap(*index: int, **kws: Any) None#

Apply FREDKIN gate on the circuit. See tensorcircuit.gates.fredkin_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j \end{bmatrix}\end{split}\]

cu(*index: int, **vars: Any) None#

Apply CU gate with parameters on the circuit. See tensorcircuit.gates.cu_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

cx(*index: int, **kws: Any) None#

Apply CNOT gate on the circuit. See tensorcircuit.gates.cnot_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j\\ 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j \end{bmatrix}\end{split}\]

cy(*index: int, **kws: Any) None#

Apply CY gate on the circuit. See tensorcircuit.gates.cy_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 0.-1.j\\ 0.+0.j & 0.+0.j & 0.+1.j & 0.+0.j \end{bmatrix}\end{split}\]

cz(*index: int, **kws: Any) None#

Apply CZ gate on the circuit. See tensorcircuit.gates.cz_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & -1.+0.j \end{bmatrix}\end{split}\]

densitymatrix(check: bool = False, reuse: bool = True) Any#

Return the output density matrix of the circuit.

参数
  • check (bool, optional) -- check whether the final return is a legal density matrix, defaults to False

  • reuse (bool, optional) -- whether to reuse previous results, defaults to True

返回

The output densitymatrix in 2D shape tensor form

返回类型

Tensor

depolarizing(*index: int, **vars: float) None#

Apply depolarizing quantum channel on the circuit. See tensorcircuit.channels.depolarizingchannel()

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the channel.

draw(**kws: Any) Any#

Visualise the circuit. This method recevies the keywords as same as qiskit.circuit.QuantumCircuit.draw. More details can be found here: https://qiskit.org/documentation/stubs/qiskit.circuit.QuantumCircuit.draw.html. Interesting kws options include: ``idle_wires``(bool)

Example

>>> c = tc.Circuit(3)
>>> c.H(1)
>>> c.X(2)
>>> c.CNOT(0, 1)
>>> c.draw(output='text')
q_0: ───────■──
     ┌───┐┌─┴─┐
q_1: ┤ H ├┤ X ├
     ├───┤└───┘
q_2: ┤ X ├─────
     └───┘
exp(*index: int, **vars: Any) None#

Apply EXP gate with parameters on the circuit. See tensorcircuit.gates.exp_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

exp1(*index: int, **vars: Any) None#

Apply EXP1 gate with parameters on the circuit. See tensorcircuit.gates.exp1_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

expectation(*ops: Tuple[tensornetwork.network_components.Node, List[int]], reuse: bool = True, noise_conf: Optional[Any] = None, status: Optional[Any] = None, **kws: Any) <property object at 0x7f7e14faaef0>#

Compute the expectation of corresponding operators.

参数
  • ops (Tuple[tn.Node, List[int]]) -- Operator and its position on the circuit, eg. (tc.gates.z(), [1, ]), (tc.gates.x(), [2, ]) is for operator \(Z_1X_2\).

  • reuse (bool) -- whether contract the density matrix in advance, defaults to True

  • noise_conf (Optional[NoiseConf], optional) -- Noise Configuration, defaults to None

  • status (Optional[Tensor], optional) -- external randomness given by tensor uniformly from [0, 1], defaults to None, used for noisfy circuit sampling

返回

Tensor with one element

返回类型

Tensor

expectation_before(*ops: Tuple[tensornetwork.network_components.Node, List[int]], reuse: bool = True, **kws: Any) List[tensornetwork.network_components.Node]#

Get the tensor network in the form of a list of nodes for the expectation calculation before the real contraction

参数

reuse (bool, optional) -- _description_, defaults to True

引发

ValueError -- _description_

返回

_description_

返回类型

List[tn.Node]

expectation_ps(x: Optional[Sequence[int]] = None, y: Optional[Sequence[int]] = None, z: Optional[Sequence[int]] = None, ps: Optional[Sequence[int]] = None, reuse: bool = True, noise_conf: Optional[Any] = None, nmc: int = 1000, status: Optional[Any] = None, **kws: Any) Any#

Shortcut for Pauli string expectation. x, y, z list are for X, Y, Z positions

Example

>>> c = tc.Circuit(2)
>>> c.X(0)
>>> c.H(1)
>>> c.expectation_ps(x=[1], z=[0])
array(-0.99999994+0.j, dtype=complex64)
>>> c = tc.Circuit(2)
>>> c.cnot(0, 1)
>>> c.rx(0, theta=0.4)
>>> c.rx(1, theta=0.8)
>>> c.h(0)
>>> c.h(1)
>>> error1 = tc.channels.generaldepolarizingchannel(0.1, 1)
>>> error2 = tc.channels.generaldepolarizingchannel(0.06, 2)
>>> noise_conf = NoiseConf()
>>> noise_conf.add_noise("rx", error1)
>>> noise_conf.add_noise("cnot", [error2], [[0, 1]])
>>> c.expectation_ps(x=[0], noise_conf=noise_conf, nmc=10000)
(0.46274087-3.764033e-09j)
参数
  • x (Optional[Sequence[int]], optional) -- sites to apply X gate, defaults to None

  • y (Optional[Sequence[int]], optional) -- sites to apply Y gate, defaults to None

  • z (Optional[Sequence[int]], optional) -- sites to apply Z gate, defaults to None

  • ps (Optional[Sequence[int]], optional) -- or one can apply a ps structures instead of x, y, z, e.g. [0, 1, 3, 0, 2, 2] for X_1Z_2Y_4Y_5 defaults to None, ps can overwrite x, y and z

  • reuse (bool, optional) -- whether to cache and reuse the wavefunction, defaults to True

  • noise_conf (Optional[NoiseConf], optional) -- Noise Configuration, defaults to None

  • nmc (int, optional) -- repetition time for Monte Carlo sampling for noisfy calculation, defaults to 1000

  • status (Optional[Tensor], optional) -- external randomness given by tensor uniformly from [0, 1], defaults to None, used for noisfy circuit sampling

返回

Expectation value

返回类型

Tensor

fredkin(*index: int, **kws: Any) None#

Apply FREDKIN gate on the circuit. See tensorcircuit.gates.fredkin_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j \end{bmatrix}\end{split}\]

classmethod from_json(jsonstr: str, circuit_params: Optional[Dict[str, Any]] = None) tensorcircuit.abstractcircuit.AbstractCircuit#

load json str as a Circuit

参数
  • jsonstr (str) -- _description_

  • circuit_params (Optional[Dict[str, Any]], optional) -- Extra circuit parameters in the format of __init__, defaults to None

返回

_description_

返回类型

AbstractCircuit

classmethod from_json_file(file: str, circuit_params: Optional[Dict[str, Any]] = None) tensorcircuit.abstractcircuit.AbstractCircuit#

load json file and convert it to a circuit

参数
  • file (str) -- filename

  • circuit_params (Optional[Dict[str, Any]], optional) -- _description_, defaults to None

返回

_description_

返回类型

AbstractCircuit

classmethod from_openqasm(qasmstr: str, circuit_params: Optional[Dict[str, Any]] = None, keep_measure_order: bool = False) tensorcircuit.abstractcircuit.AbstractCircuit#
classmethod from_openqasm_file(file: str, circuit_params: Optional[Dict[str, Any]] = None, keep_measure_order: bool = False) tensorcircuit.abstractcircuit.AbstractCircuit#
classmethod from_qir(qir: List[Dict[str, Any]], circuit_params: Optional[Dict[str, Any]] = None) tensorcircuit.abstractcircuit.AbstractCircuit#

Restore the circuit from the quantum intermediate representation.

Example

>>> c = tc.Circuit(3)
>>> c.H(0)
>>> c.rx(1, theta=tc.array_to_tensor(0.7))
>>> c.exp1(0, 1, unitary=tc.gates._zz_matrix, theta=tc.array_to_tensor(-0.2), split=split)
>>> len(c)
7
>>> c.expectation((tc.gates.z(), [1]))
array(0.764842+0.j, dtype=complex64)
>>> qirs = c.to_qir()
>>>
>>> c = tc.Circuit.from_qir(qirs, circuit_params={"nqubits": 3})
>>> len(c._nodes)
7
>>> c.expectation((tc.gates.z(), [1]))
array(0.764842+0.j, dtype=complex64)
参数
  • qir (List[Dict[str, Any]]) -- The quantum intermediate representation of a circuit.

  • circuit_params (Optional[Dict[str, Any]]) -- Extra circuit parameters.

返回

The circuit have same gates in the qir.

返回类型

Circuit

classmethod from_qiskit(qc: Any, n: Optional[int] = None, inputs: Optional[List[float]] = None, circuit_params: Optional[Dict[str, Any]] = None, binding_params: Optional[Union[Sequence[float], Dict[Any, float]]] = None) tensorcircuit.abstractcircuit.AbstractCircuit#

Import Qiskit QuantumCircuit object as a tc.Circuit object.

Example

>>> from qiskit import QuantumCircuit
>>> qisc = QuantumCircuit(3)
>>> qisc.h(2)
>>> qisc.cswap(1, 2, 0)
>>> qisc.swap(0, 1)
>>> c = tc.Circuit.from_qiskit(qisc)
参数
  • qc (QuantumCircuit in Qiskit) -- Qiskit Circuit object

  • n (int) -- The number of qubits for the circuit

  • inputs (Optional[List[float]], optional) -- possible input wavefunction for tc.Circuit, defaults to None

  • circuit_params (Optional[Dict[str, Any]]) -- kwargs given in Circuit.__init__ construction function, default to None.

  • binding_params (Optional[Union[Sequence[float], Dict[Any, float]]]) -- (variational) parameters for the circuit. Could be either a sequence or dictionary depending on the type of parameters in the Qiskit circuit. For ParameterVectorElement use sequence. For Parameter use dictionary

返回

The same circuit but as tensorcircuit object

返回类型

Circuit

classmethod from_qsim_file(file: str, circuit_params: Optional[Dict[str, Any]] = None) tensorcircuit.abstractcircuit.AbstractCircuit#
static front_from_nodes(nodes: List[tensornetwork.network_components.Node]) List[tensornetwork.network_components.Edge]#
gate_aliases = [['cnot', 'cx'], ['fredkin', 'cswap'], ['toffoli', 'ccnot'], ['toffoli', 'ccx'], ['any', 'unitary'], ['sd', 'sdg'], ['td', 'tdg']]#
gate_count(gate_list: Optional[Union[str, Sequence[str]]] = None) int#

count the gate number of the circuit

Example

>>> c = tc.Circuit(3)
>>> c.h(0)
>>> c.multicontrol(0, 1, 2, ctrl=[0, 1], unitary=tc.gates._x_matrix)
>>> c.toffolli(1, 2, 0)
>>> c.gate_count()
3
>>> c.gate_count(["multicontrol", "toffoli"])
2
参数

gate_list (Optional[Sequence[str]], optional) -- gate name or gate name list to be counted, defaults to None (counting all gates)

返回

the total number of all gates or gates in the gate_list

返回类型

int

gate_count_by_condition(cond_func: Callable[[Dict[str, Any]], bool]) int#

count the number of gates that satisfy certain condition

Example

>>> c = tc.Circuit(3)
>>> c.x(0)
>>> c.h(0)
>>> c.multicontrol(0, 1, 2, ctrl=[0, 1], unitary=tc.gates._x_matrix)
>>> c.gate_count_by_condition(lambda qir: qir["index"] == (0, ))
2
>>> c.gate_count_by_condition(lambda qir: qir["mpo"])
1
参数

cond_func (Callable[[Dict[str, Any]], bool]) -- the condition for counting the gate

返回

the total number of all gates which satisfy the condition

返回类型

int

gate_summary() Dict[str, int]#

return the summary dictionary on gate type - gate count pair

返回

the gate count dict by gate type

返回类型

Dict[str, int]

general_kraus(kraus: Sequence[tensorcircuit.gates.Gate], *index: int, **kws: Any) None#
generaldepolarizing(*index: int, **vars: float) None#

Apply generaldepolarizing quantum channel on the circuit. See tensorcircuit.channels.generaldepolarizingchannel()

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the channel.

get_dm_as_quoperator() tensorcircuit.quantum.QuOperator#

Get the representation of the output state in the form of QuOperator while maintaining the circuit uncomputed

返回

QuOperator representation of the output state from the circuit

返回类型

QuOperator

get_dm_as_quvector() tensorcircuit.quantum.QuVector#

Get the representation of the output state in the form of QuVector while maintaining the circuit uncomputed

返回

QuVector representation of the output state from the circuit

返回类型

QuVector

get_positional_logical_mapping() Dict[int, int]#

Get positional logical mapping dict based on measure instruction. This function is useful when we only measure part of the qubits in the circuit, to process the count result from partial measurement, we must be aware of the mapping, i.e. for each position in the count bitstring, what is the corresponding qubits (logical) defined on the circuit

返回

positional_logical_mapping

返回类型

Dict[int, int]

get_quvector() tensorcircuit.quantum.QuVector#

Get the representation of the output state in the form of QuVector while maintaining the circuit uncomputed

返回

QuVector representation of the output state from the circuit

返回类型

QuVector

h(*index: int, **kws: Any) None#

Apply H gate on the circuit. See tensorcircuit.gates.h_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 0.70710677+0.j & 0.70710677+0.j\\ 0.70710677+0.j & -0.70710677+0.j \end{bmatrix}\end{split}\]

i(*index: int, **kws: Any) None#

Apply I gate on the circuit. See tensorcircuit.gates.i_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 1.+0.j & 0.+0.j\\ 0.+0.j & 1.+0.j \end{bmatrix}\end{split}\]

initial_mapping(logical_physical_mapping: Dict[int, int], n: Optional[int] = None, circuit_params: Optional[Dict[str, Any]] = None) tensorcircuit.abstractcircuit.AbstractCircuit#

generate a new circuit with the qubit mapping given by logical_physical_mapping

参数
  • logical_physical_mapping (Dict[int, int]) -- how to map logical qubits to the physical qubits on the new circuit

  • n (Optional[int], optional) -- number of qubit of the new circuit, can be different from the original one, defaults to None

  • circuit_params (Optional[Dict[str, Any]], optional) -- _description_, defaults to None

返回

_description_

返回类型

AbstractCircuit

inputs: Any#
inverse(circuit_params: Optional[Dict[str, Any]] = None) tensorcircuit.abstractcircuit.AbstractCircuit#

inverse the circuit, return a new inversed circuit

EXAMPLE

>>> c = tc.Circuit(2)
>>> c.H(0)
>>> c.rzz(1, 2, theta=0.8)
>>> c1 = c.inverse()
参数

circuit_params (Optional[Dict[str, Any]], optional) -- keywords dict for initialization the new circuit, defaults to None

返回

the inversed circuit

返回类型

Circuit

is_dm: bool = True#
is_mps: bool = False#
isotropicdepolarizing(*index: int, **vars: float) None#

Apply isotropicdepolarizing quantum channel on the circuit. See tensorcircuit.channels.isotropicdepolarizingchannel()

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the channel.

iswap(*index: int, **vars: Any) None#

Apply ISWAP gate with parameters on the circuit. See tensorcircuit.gates.iswap_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

measure(*index: int, with_prob: bool = False, status: Optional[Any] = None) Tuple[Any, Any]#

Take measurement to the given quantum lines. This method is jittable is and about 100 times faster than unjit version!

参数
  • index (int) -- Measure on which quantum line.

  • with_prob (bool, optional) -- If true, theoretical probability is also returned.

  • status (Optional[Tensor]) -- external randomness, with shape [index], defaults to None

返回

The sample output and probability (optional) of the quantum line.

返回类型

Tuple[Tensor, Tensor]

measure_instruction(*index: int) None#

add a measurement instruction flag, no effect on numerical simulation

参数

index (int) -- the corresponding qubits

measure_jit(*index: int, with_prob: bool = False, status: Optional[Any] = None) Tuple[Any, Any]#

Take measurement to the given quantum lines. This method is jittable is and about 100 times faster than unjit version!

参数
  • index (int) -- Measure on which quantum line.

  • with_prob (bool, optional) -- If true, theoretical probability is also returned.

  • status (Optional[Tensor]) -- external randomness, with shape [index], defaults to None

返回

The sample output and probability (optional) of the quantum line.

返回类型

Tuple[Tensor, Tensor]

mpo(*index: int, **vars: Any) None#

Apply mpo gate in MPO format on the circuit. See tensorcircuit.gates.mpo_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

mpogates = ['multicontrol', 'mpo']#
multicontrol(*index: int, **vars: Any) None#

Apply multicontrol gate in MPO format on the circuit. See tensorcircuit.gates.multicontrol_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

orx(*index: int, **vars: Any) None#

Apply ORX gate with parameters on the circuit. See tensorcircuit.gates.orx_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

ory(*index: int, **vars: Any) None#

Apply ORY gate with parameters on the circuit. See tensorcircuit.gates.ory_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

orz(*index: int, **vars: Any) None#

Apply ORZ gate with parameters on the circuit. See tensorcircuit.gates.orz_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

ox(*index: int, **kws: Any) None#

Apply OX gate on the circuit. See tensorcircuit.gates.ox_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j\\ 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j \end{bmatrix}\end{split}\]

oy(*index: int, **kws: Any) None#

Apply OY gate on the circuit. See tensorcircuit.gates.oy_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 0.+0.j & 0.-1.j & 0.+0.j & 0.+0.j\\ 0.+1.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j \end{bmatrix}\end{split}\]

oz(*index: int, **kws: Any) None#

Apply OZ gate on the circuit. See tensorcircuit.gates.oz_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & -1.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j \end{bmatrix}\end{split}\]

perfect_sampling(status: Optional[Any] = None) Tuple[str, float]#

Sampling bistrings from the circuit output based on quantum amplitudes. Reference: arXiv:1201.3974.

参数

status (Optional[Tensor]) -- external randomness, with shape [nqubits], defaults to None

返回

Sampled bit string and the corresponding theoretical probability.

返回类型

Tuple[str, float]

phase(*index: int, **vars: Any) None#

Apply PHASE gate with parameters on the circuit. See tensorcircuit.gates.phase_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

phasedamping(*index: int, **vars: float) None#

Apply phasedamping quantum channel on the circuit. See tensorcircuit.channels.phasedampingchannel()

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the channel.

prepend(c: tensorcircuit.abstractcircuit.AbstractCircuit) tensorcircuit.abstractcircuit.AbstractCircuit#

prepend circuit c before

参数

c (BaseCircuit) -- The other circuit to be prepended

返回

The composed circuit

返回类型

BaseCircuit

probability() Any#

get the 2^n length probability vector over computational basis

返回

probability vector

返回类型

Tensor

quvector() tensorcircuit.quantum.QuVector#

Get the representation of the output state in the form of QuVector while maintaining the circuit uncomputed

返回

QuVector representation of the output state from the circuit

返回类型

QuVector

r(*index: int, **vars: Any) None#

Apply R gate with parameters on the circuit. See tensorcircuit.gates.r_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

readouterror_bs(readout_error: Optional[Sequence[Any]] = None, p: Optional[Any] = None) Any#

Apply readout error to original probabilities of bit string and return the noisy probabilities.

Example

>>> readout_error = []
>>> readout_error.append([0.9,0.75])  # readout error for qubit 0, [p0|0,p1|1]
>>> readout_error.append([0.4,0.7])   # readout error for qubit 1, [p0|0,p1|1]
参数
  • readout_error (Optional[Sequence[Any]] Tensor, List, Tuple) -- list of readout error for each qubits.

  • p (Optional[Any]) -- probabilities of bit string

返回类型

Tensor

replace_inputs(inputs: Any) None#

Replace the input state with the circuit structure unchanged.

参数

inputs (Tensor) -- Input wavefunction.

reset(*index: int, **vars: float) None#

Apply reset quantum channel on the circuit. See tensorcircuit.channels.resetchannel()

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the channel.

reset_instruction(*index: int) None#

add a reset instruction flag, no effect on numerical simulation

参数

index (int) -- the corresponding qubits

rx(*index: int, **vars: Any) None#

Apply RX gate with parameters on the circuit. See tensorcircuit.gates.rx_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

rxx(*index: int, **vars: Any) None#

Apply RXX gate with parameters on the circuit. See tensorcircuit.gates.rxx_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

ry(*index: int, **vars: Any) None#

Apply RY gate with parameters on the circuit. See tensorcircuit.gates.ry_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

ryy(*index: int, **vars: Any) None#

Apply RYY gate with parameters on the circuit. See tensorcircuit.gates.ryy_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

rz(*index: int, **vars: Any) None#

Apply RZ gate with parameters on the circuit. See tensorcircuit.gates.rz_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

rzz(*index: int, **vars: Any) None#

Apply RZZ gate with parameters on the circuit. See tensorcircuit.gates.rzz_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

s(*index: int, **kws: Any) None#

Apply S gate on the circuit. See tensorcircuit.gates.s_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 1.+0.j & 0.+0.j\\ 0.+0.j & 0.+1.j \end{bmatrix}\end{split}\]

sample(batch: Optional[int] = None, allow_state: bool = False, readout_error: Optional[Sequence[Any]] = None, format: Optional[str] = None, random_generator: Optional[Any] = None, status: Optional[Any] = None) Any#

batched sampling from state or circuit tensor network directly

参数
  • batch (Optional[int], optional) -- number of samples, defaults to None

  • allow_state (bool, optional) -- if true, we sample from the final state if memory allows, True is preferred, defaults to False

  • readout_error (Optional[Sequence[Any]] Tensor, List, Tuple) -- readout_error, defaults to None

  • format (Optional[str]) -- sample format, defaults to None as backward compatibility check the doc in tensorcircuit.quantum.measurement_results()

  • format -- alias for the argument format

  • random_generator (Optional[Any], optional) -- random generator, defaults to None

  • status (Optional[Tensor]) -- external randomness given by tensor uniformly from [0, 1], if set, can overwrite random_generator

返回

List (if batch) of tuple (binary configuration tensor and corresponding probability) if the format is None, and consistent with format when given

返回类型

Any

sample_expectation_ps(x: Optional[Sequence[int]] = None, y: Optional[Sequence[int]] = None, z: Optional[Sequence[int]] = None, shots: Optional[int] = None, random_generator: Optional[Any] = None, status: Optional[Any] = None, readout_error: Optional[Sequence[Any]] = None, noise_conf: Optional[Any] = None, nmc: int = 1000, statusc: Optional[Any] = None, **kws: Any) Any#

Compute the expectation with given Pauli string with measurement shots numbers

Example

>>> c = tc.Circuit(2)
>>> c.H(0)
>>> c.rx(1, theta=np.pi/2)
>>> c.sample_expectation_ps(x=[0], y=[1])
-0.99999976
>>> readout_error = []
>>> readout_error.append([0.9,0.75])
>>> readout_error.append([0.4,0.7])
>>> c.sample_expectation_ps(x=[0], y=[1],readout_error = readout_error)
>>> c = tc.Circuit(2)
>>> c.cnot(0, 1)
>>> c.rx(0, theta=0.4)
>>> c.rx(1, theta=0.8)
>>> c.h(0)
>>> c.h(1)
>>> error1 = tc.channels.generaldepolarizingchannel(0.1, 1)
>>> error2 = tc.channels.generaldepolarizingchannel(0.06, 2)
>>> readout_error = [[0.9, 0.75],[0.4, 0.7]]
>>> noise_conf = NoiseConf()
>>> noise_conf.add_noise("rx", error1)
>>> noise_conf.add_noise("cnot", [error2], [[0, 1]])
>>> noise_conf.add_noise("readout", readout_error)
>>> c.sample_expectation_ps(x=[0], noise_conf=noise_conf, nmc=10000)
0.44766843
参数
  • x (Optional[Sequence[int]], optional) -- index for Pauli X, defaults to None

  • y (Optional[Sequence[int]], optional) -- index for Pauli Y, defaults to None

  • z (Optional[Sequence[int]], optional) -- index for Pauli Z, defaults to None

  • shots (Optional[int], optional) -- number of measurement shots, defaults to None, indicating analytical result

  • random_generator (Optional[Any]) -- random_generator, defaults to None

  • status (Optional[Tensor]) -- external randomness given by tensor uniformly from [0, 1], if set, can overwrite random_generator

  • readout_error (Optional[Sequence[Any]] Tensor, List, Tuple) -- readout_error, defaults to None. Overrided if noise_conf is provided.

  • noise_conf (Optional[NoiseConf], optional) -- Noise Configuration, defaults to None

  • nmc (int, optional) -- repetition time for Monte Carlo sampling for noisfy calculation, defaults to 1000

  • statusc (Optional[Tensor], optional) -- external randomness given by tensor uniformly from [0, 1], defaults to None, used for noisfy circuit sampling

返回

[description]

返回类型

Tensor

sd(*index: int, **kws: Any) None#

Apply SD gate on the circuit. See tensorcircuit.gates.sd_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 1.+0.j & 0.+0.j\\ 0.+0.j & 0.-1.j \end{bmatrix}\end{split}\]

sdg(*index: int, **kws: Any) None#

Apply SD gate on the circuit. See tensorcircuit.gates.sd_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 1.+0.j & 0.+0.j\\ 0.+0.j & 0.-1.j \end{bmatrix}\end{split}\]

select_gate(which: Any, kraus: Sequence[tensorcircuit.gates.Gate], *index: int) None#

Apply which-th gate from kraus list, i.e. apply kraus[which]

参数
  • which (Tensor) -- Tensor of shape [] and dtype int

  • kraus (Sequence[Gate]) -- A list of gate in the form of tc.gate or Tensor

  • index (int) -- the qubit lines the gate applied on

sexpps(x: Optional[Sequence[int]] = None, y: Optional[Sequence[int]] = None, z: Optional[Sequence[int]] = None, shots: Optional[int] = None, random_generator: Optional[Any] = None, status: Optional[Any] = None, readout_error: Optional[Sequence[Any]] = None, noise_conf: Optional[Any] = None, nmc: int = 1000, statusc: Optional[Any] = None, **kws: Any) Any#

Compute the expectation with given Pauli string with measurement shots numbers

Example

>>> c = tc.Circuit(2)
>>> c.H(0)
>>> c.rx(1, theta=np.pi/2)
>>> c.sample_expectation_ps(x=[0], y=[1])
-0.99999976
>>> readout_error = []
>>> readout_error.append([0.9,0.75])
>>> readout_error.append([0.4,0.7])
>>> c.sample_expectation_ps(x=[0], y=[1],readout_error = readout_error)
>>> c = tc.Circuit(2)
>>> c.cnot(0, 1)
>>> c.rx(0, theta=0.4)
>>> c.rx(1, theta=0.8)
>>> c.h(0)
>>> c.h(1)
>>> error1 = tc.channels.generaldepolarizingchannel(0.1, 1)
>>> error2 = tc.channels.generaldepolarizingchannel(0.06, 2)
>>> readout_error = [[0.9, 0.75],[0.4, 0.7]]
>>> noise_conf = NoiseConf()
>>> noise_conf.add_noise("rx", error1)
>>> noise_conf.add_noise("cnot", [error2], [[0, 1]])
>>> noise_conf.add_noise("readout", readout_error)
>>> c.sample_expectation_ps(x=[0], noise_conf=noise_conf, nmc=10000)
0.44766843
参数
  • x (Optional[Sequence[int]], optional) -- index for Pauli X, defaults to None

  • y (Optional[Sequence[int]], optional) -- index for Pauli Y, defaults to None

  • z (Optional[Sequence[int]], optional) -- index for Pauli Z, defaults to None

  • shots (Optional[int], optional) -- number of measurement shots, defaults to None, indicating analytical result

  • random_generator (Optional[Any]) -- random_generator, defaults to None

  • status (Optional[Tensor]) -- external randomness given by tensor uniformly from [0, 1], if set, can overwrite random_generator

  • readout_error (Optional[Sequence[Any]] Tensor, List, Tuple) -- readout_error, defaults to None. Overrided if noise_conf is provided.

  • noise_conf (Optional[NoiseConf], optional) -- Noise Configuration, defaults to None

  • nmc (int, optional) -- repetition time for Monte Carlo sampling for noisfy calculation, defaults to 1000

  • statusc (Optional[Tensor], optional) -- external randomness given by tensor uniformly from [0, 1], defaults to None, used for noisfy circuit sampling

返回

[description]

返回类型

Tensor

sgates = ['i', 'x', 'y', 'z', 'h', 't', 's', 'td', 'sd', 'wroot', 'cnot', 'cz', 'swap', 'cy', 'ox', 'oy', 'oz', 'toffoli', 'fredkin']#
split: Optional[Dict[str, Any]]#
static standardize_gate(name: str) str#

standardize the gate name to tc common gate sets

参数

name (str) -- non-standard gate name

返回

the standard gate name

返回类型

str

state(check: bool = False, reuse: bool = True) Any#

Return the output density matrix of the circuit.

参数
  • check (bool, optional) -- check whether the final return is a legal density matrix, defaults to False

  • reuse (bool, optional) -- whether to reuse previous results, defaults to True

返回

The output densitymatrix in 2D shape tensor form

返回类型

Tensor

swap(*index: int, **kws: Any) None#

Apply SWAP gate on the circuit. See tensorcircuit.gates.swap_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j\\ 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j \end{bmatrix}\end{split}\]

t(*index: int, **kws: Any) None#

Apply T gate on the circuit. See tensorcircuit.gates.t_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 1. & +0.j & 0. & +0.j\\ 0. & +0.j & 0.70710677+0.70710677j \end{bmatrix}\end{split}\]

td(*index: int, **kws: Any) None#

Apply TD gate on the circuit. See tensorcircuit.gates.td_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 1. & +0.j & 0. & +0.j\\ 0. & +0.j & 0.70710677-0.70710677j \end{bmatrix}\end{split}\]

tdg(*index: int, **kws: Any) None#

Apply TD gate on the circuit. See tensorcircuit.gates.td_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 1. & +0.j & 0. & +0.j\\ 0. & +0.j & 0.70710677-0.70710677j \end{bmatrix}\end{split}\]

tex(**kws: Any) str#

Generate latex string based on quantikz latex package

返回

Latex string that can be directly compiled via, e.g. latexit

返回类型

str

thermalrelaxation(*index: int, **vars: float) None#

Apply thermalrelaxation quantum channel on the circuit. See tensorcircuit.channels.thermalrelaxationchannel()

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the channel.

to_circuit(circuit_params: Optional[Dict[str, Any]] = None) tensorcircuit.circuit.Circuit#

convert into state simulator (current implementation ignores all noise channels)

参数

circuit_params (Optional[Dict[str, Any]], optional) -- kws to initialize circuit object, defaults to None

返回

Circuit with no noise

返回类型

Circuit

to_cirq(enable_instruction: bool = False) Any#

Translate tc.Circuit to a cirq circuit object.

参数

enable_instruction (bool, defaults to False) -- whether also export measurement and reset instructions

返回

A cirq circuit of this circuit.

to_graphviz(graph: Optional[graphviz.graphs.Graph] = None, include_all_names: bool = False, engine: str = 'neato') graphviz.graphs.Graph#

Not an ideal visualization for quantum circuit, but reserve here as a general approach to show the tensornetwork [Deprecated, use Circuit.vis_tex or Circuit.draw instead]

to_json(file: Optional[str] = None, simplified: bool = False) Any#

circuit dumps to json

参数
  • file (Optional[str], optional) -- file str to dump the json to, defaults to None, return the json str

  • simplified (bool) -- If False, keep all info for each gate, defaults to be False. If True, suitable for IO since less information is required

返回

None if dumps to file otherwise the json str

返回类型

Any

to_openqasm(**kws: Any) str#

transform circuit to openqasm via qiskit circuit, see https://qiskit.org/documentation/stubs/qiskit.circuit.QuantumCircuit.qasm.html for usage on possible options for kws

返回

circuit representation in openqasm format

返回类型

str

to_qir() List[Dict[str, Any]]#

Return the quantum intermediate representation of the circuit.

Example

>>> c = tc.Circuit(2)
>>> c.CNOT(0, 1)
>>> c.to_qir()
[{'gatef': cnot, 'gate': Gate(
    name: 'cnot',
    tensor:
        array([[[[1.+0.j, 0.+0.j],
                [0.+0.j, 0.+0.j]],

                [[0.+0.j, 1.+0.j],
                [0.+0.j, 0.+0.j]]],


            [[[0.+0.j, 0.+0.j],
                [0.+0.j, 1.+0.j]],

                [[0.+0.j, 0.+0.j],
                [1.+0.j, 0.+0.j]]]], dtype=complex64),
    edges: [
        Edge(Dangling Edge)[0],
        Edge(Dangling Edge)[1],
        Edge('cnot'[2] -> 'qb-1'[0] ),
        Edge('cnot'[3] -> 'qb-2'[0] )
    ]), 'index': (0, 1), 'name': 'cnot', 'split': None, 'mpo': False}]
返回

The quantum intermediate representation of the circuit.

返回类型

List[Dict[str, Any]]

to_qiskit(enable_instruction: bool = False, enable_inputs: bool = False) Any#

Translate tc.Circuit to a qiskit QuantumCircuit object.

参数
  • enable_instruction (bool, defaults to False) -- whether also export measurement and reset instructions

  • enable_inputs (bool, defaults to False) -- whether also export the inputs

返回

A qiskit object of this circuit.

toffoli(*index: int, **kws: Any) None#

Apply TOFFOLI gate on the circuit. See tensorcircuit.gates.toffoli_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j & 0.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j\\ 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 0.+0.j & 1.+0.j & 0.+0.j \end{bmatrix}\end{split}\]

u(*index: int, **vars: Any) None#

Apply U gate with parameters on the circuit. See tensorcircuit.gates.u_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

unitary(*index: int, **vars: Any) None#

Apply ANY gate with parameters on the circuit. See tensorcircuit.gates.any_gate().

参数
  • index (int.) -- Qubit number that the gate applies on.

  • vars (float.) -- Parameters for the gate.

vgates = ['r', 'cr', 'u', 'cu', 'rx', 'ry', 'rz', 'phase', 'rxx', 'ryy', 'rzz', 'cphase', 'crx', 'cry', 'crz', 'orx', 'ory', 'orz', 'iswap', 'any', 'exp', 'exp1']#
vis_tex(**kws: Any) str#

Generate latex string based on quantikz latex package

返回

Latex string that can be directly compiled via, e.g. latexit

返回类型

str

wavefunction() Any#

get the wavefunction of outputs, raise error if the final state is not purified [Experimental: the phase factor is not fixed for different backend]

返回

wavefunction vector

返回类型

Tensor

wroot(*index: int, **kws: Any) None#

Apply WROOT gate on the circuit. See tensorcircuit.gates.wroot_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 0.70710677+0.j & -0.5 & -0.5j\\ 0.5 & -0.5j & 0.70710677+0.j \end{bmatrix}\end{split}\]

x(*index: int, **kws: Any) None#

Apply X gate on the circuit. See tensorcircuit.gates.x_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 0.+0.j & 1.+0.j\\ 1.+0.j & 0.+0.j \end{bmatrix}\end{split}\]

y(*index: int, **kws: Any) None#

Apply Y gate on the circuit. See tensorcircuit.gates.y_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 0.+0.j & 0.-1.j\\ 0.+1.j & 0.+0.j \end{bmatrix}\end{split}\]

z(*index: int, **kws: Any) None#

Apply Z gate on the circuit. See tensorcircuit.gates.z_gate().

参数

index (int.) --

Qubit number that the gate applies on. The matrix for the gate is

\[\begin{split}\begin{bmatrix} 1.+0.j & 0.+0.j\\ 0.+0.j & -1.+0.j \end{bmatrix}\end{split}\]