quri_parts.core package

class Operator

Bases: dict[PauliLabel, complex]

Operator represents the set of single-term operators as a dict[PauliLabel, coefficient].

Coefficients can not only be real values but also complex values since Operator represents a general operator including non-Hermitian one.

Example

>>> op = Operator({pauli_label("X0"): 0.1j})
>>> op
{PauliLabel({(0, <SinglePauli.X: 1>)}): 0.1j}
>>> op[pauli_label("X1 Y2")] = 0.2
>>> op
{PauliLabel({(0, <SinglePauli.X: 1>)}): 0.1j, PauliLabel({(1, <SinglePauli.X: 1>), (2, <SinglePauli.Y: 2>)}): 0.2}
>>> str(op)
'0.1j*X0 + 0.2*X1 Y2'
>>> for pauli, coef in op.items():
...     print(f"Pauli: {pauli}, coefficient: {coef}")
...
Pauli: X0, coefficient: 0.1j
Pauli: X1 Y2, coefficient: 0.2
>>> op.constant
0.0
>>> op[PAULI_IDENTITY] = 2.0
>>> op
{PauliLabel({(0, <SinglePauli.X: 1>)}): 0.1j, PauliLabel({(1, <SinglePauli.X: 1>), (2, <SinglePauli.Y: 2>)}): 0.2, PauliLabel(): 2.0}
>>> op.constant
2.0
>>> op.constant = 1.0
>>> op.constant
1.0
You can add a single-term operator by add_term() method.
>>> operator.add_term(PauliLabel({(1, 1)}), 1.0)
By accessing by key, the coefficient is replaced with a new value.
>>> operator = Operator({pauli_label("X0"): 0.1j})
>>> op
{PauliLabel({(0, <SinglePauli.X: 1>)}): 0.1j}
>>> operator[pauli_label("X0")] = 0.1
>>> op
{PauliLabel({(0, <SinglePauli.X: 1>)}): 0.1}
add_term(pauli_label, coef)

Method for adding single-term operator.

Parameters:
Return type:

None

copy()

Returns the copy of itself.

Return type:

Operator

property n_terms: int

Number of all terms.

property constant: complex

Constant value.

Note that the constant value is a coefficient of an identity pauli term (PAULI_IDENTITY).

hermitian_conjugated()
Return type:

Operator

pauli_label(p)

Create a PauliLabel.

The argument can be one of the followings:

  • A string representation of a Pauli string.

  • A PauliLabelProvider, an object providing a qubit index list and a SinglePauli list.

  • An Iterable of tuple[int, int], pairs of a qubit index and a SinglePauli.

Parameters:

p (str | PauliLabelProvider | Iterable[tuple[int, int]])

Return type:

PauliLabel

get_sparse_matrix(operator, n_qubits=None, format='csc')

Convert PauliLabel and Operator into scipy sparse matrix.

Parameters:
  • operator (PauliLabel | Operator)

  • n_qubits (int | None)

  • format (core.operator.sparse.SparseMatrixName)

Return type:

spmatrix

quantum_state(n_qubits, *, vector=None, bits=0, circuit=None)

Returns a quantum state generated by a given vector, bits, and a circuit.

Raises ValueError if both a vector and bits input at the same time.

Parameters:
Return type:

QuantumState

apply_circuit(circuit, state)

Returns a new state with the circuit applied.

The original state is not changed.

Parameters:
Return type:

QuantumState

class QuantumCircuit(qubit_count, cbit_count=None, gates=Ellipsis)

Bases: ImmutableQuantumCircuit

add_CNOT_gate(control_index, target_index)
add_CZ_gate(control_index, target_index)
add_H_gate(qubit_index)
add_Identity_gate(qubit_index)
add_PauliRotation_gate(target_qubits, pauli_id_list, angle)
add_Pauli_gate(target_indices, pauli_ids)
add_RX_gate(qubit_index, angle)
add_RY_gate(qubit_index, angle)
add_RZ_gate(qubit_index, angle)
add_SWAP_gate(target_index1, target_index2)
add_S_gate(qubit_index)
add_Sdag_gate(qubit_index)
add_SingleQubitUnitaryMatrix_gate(target_index, unitary_matrix)
add_SqrtX_gate(qubit_index)
add_SqrtXdag_gate(qubit_index)
add_SqrtY_gate(qubit_index)
add_SqrtYdag_gate(qubit_index)
add_TOFFOLI_gate(control_index1, control_index2, target_index)
add_T_gate(qubit_index)
add_Tdag_gate(qubit_index)
add_TwoQubitUnitaryMatrix_gate(target_index1, target_index2, unitary_matrix)
add_U1_gate(qubit_index, lmd)
add_U2_gate(qubit_index, phi, lmd)
add_U3_gate(qubit_index, theta, phi, lmd)
add_UnitaryMatrix_gate(target_indices, unitary_matrix)
add_X_gate(qubit_index)
add_Y_gate(qubit_index)
add_Z_gate(qubit_index)
add_gate()
extend()
measure(qubit_indices, classical_indices)
draw_circuit(circuit, line_length=80)

Circuit drawer which outputs given circuit as an ASCII art to standard streams.

Parameters:
Return type:

None

Subpackages