circuit#
Quantum circuit
Classes
|
Quantum circuit for a distributed state vector. |
|
Quantum circuit for qubits. |
- class DistributedQubitCircuit(nqubit: int, name: str | None = None, reupload: bool = False, shots: int = 1024)[source]#
Bases:
QubitCircuitQuantum circuit for a distributed state vector.
- Parameters:
nqubit (int) – The number of qubits in the circuit.
name (str | None) – The name of the circuit. Default:
Nonereupload (bool) – Whether to use data re-uploading. Default:
Falseshots (int) – The number of shots for the measurement. Default: 1024
- expectation(shots: int | None = None) Tensor[source]#
Get the expectation value according to the final state and
observables.- Parameters:
shots (int | None) – The number of shots for the expectation value. Default:
None(which means the exact and differentiable expectation value)
- forward(data: Tensor | None = None, state: DistributedQubitState | None = None) DistributedQubitState[source]#
Perform a forward pass of the quantum circuit and return the final state.
This method applies the
operatorsof the quantum circuit to the initial state or the given state and returns the resulting state. Ifdatais given, it is used as the input for theencoders. Thedatamust be a 1D tensor.- Parameters:
data (Tensor | None) – The input data for the
encoders. Default:Nonestate (DistributedQubitState | None) – The initial state for the quantum circuit. Default:
None
- measure(shots: int | None = None, with_prob: bool = False, wires: int | list[int] | None = None, block_size: int = 16777216) dict | None[source]#
Measure the final state.
- Parameters:
shots (int | None) – The number of shots for the measurement. Default:
None(which meansself.shots)with_prob (bool) – Whether to show the true probability of the measurement. Default:
Falsewires (int | list[int] | None) – The wires to measure. Default:
None(which means all wires)block_size (int) – The block size for sampling. Default: 2**24
- set_init_state(init_state: str | DistributedQubitState = 'zeros') None[source]#
Set the initial state of the circuit.
- class QubitCircuit(nqubit: int, init_state: Any = 'zeros', name: str | None = None, den_mat: bool = False, reupload: bool = False, mps: bool = False, chi: int | None = None, shots: int = 1024)[source]#
Bases:
OperationQuantum circuit for qubits.
This class inherits from the
Operationclass and implements methods for creating, manipulating, and measuring quantum states.- Parameters:
nqubit (int) – The number of qubits in the circuit.
init_state (Any) – The initial state of the circuit. Default:
'zeros'name (str | None) – The name of the circuit. Default:
Noneden_mat (bool) – Whether to use density matrix representation. Default:
Falsereupload (bool) – Whether to use data re-uploading. Default:
Falsemps (bool) – Whether to use matrix product state representation. Default:
Falsechi (int | None) – The bond dimension for matrix product state representation. Default:
Noneshots (int) – The number of shots for the measurement. Default: 1024
- Raises:
AssertionError – If the type or dimension of
init_statedoes not matchnqubitorden_mat.
- add(op: Operation, encode: bool = False, wires: int | list[int] | None = None, controls: int | list[int] | None = None) None[source]#
A method that adds an operation to the quantum circuit.
The operation can be a gate, a layer, or another quantum circuit. The method also updates the attributes of the quantum circuit. If
wiresis specified, the parameters of gates are shared.- Parameters:
op (Operation) – The operation to add. It is an instance of
Operationclass or its subclasses, such asGate,Layer,Channel, orQubitCircuit.encode (bool) – Whether the gate or layer is to encode data. Default:
Falsewires (int | list[int] | None) – The wires to apply the gate on. It can be an integer or a list of integers specifying the indices of the wires. Default:
None(which means the gate has its own wires)controls (int | list[int] | None) – The control wires for the gate. It can be an integer or a list of integers specifying the indices of the control wires. Only valid when
wiresis notNone. Default:None(which means the gate has its own control wires)
- Raises:
AssertionError – If the input arguments are invalid or incompatible with the quantum circuit.
- amp_damp(wires: int, inputs: Any = None, encode: bool = False) None[source]#
Add an amplitude-damping channel.
- amplitude_encoding(data: Any) Tensor[source]#
Encode data into quantum states using amplitude encoding.
- any(unitary: Any, wires: int | list[int] | None = None, minmax: list[int] | None = None, controls: int | list[int] | None = None, name: str = 'uany') None[source]#
Add an arbitrary unitary gate.
- bit_flip(wires: int, inputs: Any = None, encode: bool = False) None[source]#
Add a bit-flip channel.
- cnot_ring(minmax: list[int] | None = None, step: int = 1, reverse: bool = False) None[source]#
Add a layer of CNOT gates in a cyclic way.
- cp(control: int, target: int, inputs: Any = None, encode: bool = False) None[source]#
Add a controlled phase shift gate.
- crx(control: int, target: int, inputs: Any = None, encode: bool = False) None[source]#
Add a controlled Rx gate.
- crxx(control: int, target1: int, target2: int, inputs: Any = None, encode: bool = False) None[source]#
Add a controlled Rxx gate.
- crxy(control: int, target1: int, target2: int, inputs: Any = None, encode: bool = False) None[source]#
Add a controlled Rxy gate.
- cry(control: int, target: int, inputs: Any = None, encode: bool = False) None[source]#
Add a controlled Ry gate.
- cryy(control: int, target1: int, target2: int, inputs: Any = None, encode: bool = False) None[source]#
Add a controlled Ryy gate.
- crz(control: int, target: int, inputs: Any = None, encode: bool = False) None[source]#
Add a controlled Rz gate.
- crzz(control: int, target1: int, target2: int, inputs: Any = None, encode: bool = False) None[source]#
Add a controlled Rzz gate.
- cu(control: int, target: int, inputs: Any = None, encode: bool = False) None[source]#
Add a controlled U3 gate.
- defer_measure(with_prob: bool = False) Tensor | tuple[Tensor, list, list][source]#
Get the state vectors and the measurement results after deferred measurement.
- depolarizing(wires: int, inputs: Any = None, encode: bool = False) None[source]#
Add a depolarizing channel.
- encode(data: Tensor | None) None[source]#
Encode the input data into the quantum circuit parameters.
This method iterates over the
encodersof the quantum circuit and initializes their parameters with the input data. IfreuploadisFalse, the input data must be at least as long as the number of parameters in theencoders. IfreuploadisTrue, the input data can be repeated to fill up the parameters.- Parameters:
data (Tensor | None) – The input data for the
encoders, must be a 1D tensor.- Raises:
AssertionError – If
reuploadisFalseand the input data is shorter than the number of parameters in theencoders.
- expectation(shots: int | None = None) Tensor[source]#
Get the expectation value according to the final state and
observables.- Parameters:
shots (int | None) – The number of shots for the expectation value. Default:
None(which means the exact and differentiable expectation value)
- forward(data: Tensor | None = None, state: Tensor | QubitState | list[Tensor] | MatrixProductState | None = None) Tensor | list[Tensor][source]#
Perform a forward pass of the quantum circuit and return the final state.
This method applies the
operatorsof the quantum circuit to the initial state or the given state and returns the resulting state. Ifdatais given, it is used as the input for theencoders. Thestatecan be either aMatrixProductStateor aQubitStateobject, or a tensor representation of them. Thedatamust be a 1D or 2D tensor.- Parameters:
data (Tensor | None) – The input data for the
encoders. Default:Nonestate (Tensor | QubitState | list[Tensor] | MatrixProductState | None) – The initial state for the quantum circuit. Default:
None
- Returns:
The final state of the quantum circuit after applying the
operators.- Return type:
Tensor | list[Tensor]
- gen_amp_damp(wires: int, inputs: Any = None, encode: bool = False) None[source]#
Add a generalized amplitude-damping channel.
- get_amplitude(bits: str) Tensor[source]#
Get the amplitude for the given bit string.
- Parameters:
bits (str) – A bit string.
- get_prob(bits: str, wires: int | list[int] | None = None) Tensor[source]#
Get the probability for the given bit string.
- Parameters:
bits (str) – A bit string.
wires (int | list[int] | None) – The wires to measure. It can be an integer or a list of integers specifying the indices of the wires.
- get_subexperiments(qubit_labels: Sequence[Hashable] | None = None) tuple[dict, list[float]][source]#
Generate cutting subexperiments and their associated coefficients.
- h(wires: int, controls: int | list[int] | None = None, condition: bool = False) None[source]#
Add a Hadamard gate.
- hamiltonian(hamiltonian: Any, t: Any = None, wires: int | list[int] | None = None, minmax: list[int] | None = None, controls: int | list[int] | None = None, encode: bool = False, name: str = 'hamiltonian') None[source]#
Add a Hamiltonian gate.
- inverse(encode: bool = False) QubitCircuit[source]#
Get the inversed circuit.
Note
The inversed circuit shares the parameters with the original circuit. You should ONLY encode data onto the original circuit. If you want to encode data onto the inversed circuit, set
encodeto beTrue.
- iswap(wires: list[int], controls: int | list[int] | None = None, condition: bool = False) None[source]#
Add an imaginary SWAP gate.
- j(wires: int, inputs: Any = None, plane: str = 'xy', controls: int | list[int] | None = None, condition: bool = False, encode: bool = False) None[source]#
Add a projection matrix J.
- latent(wires: int | list[int] | None = None, minmax: list[int] | None = None, inputs: Any = None, controls: int | list[int] | None = None, encode: bool = False, name: str = 'latent') None[source]#
Add a latent gate.
- property max_depth: int#
Get the max number of gates on the wires.
- measure(shots: int | None = None, with_prob: bool = False, wires: int | list[int] | None = None, block_size: int = 16777216) dict | list[dict] | None[source]#
Measure the final state.
- Parameters:
shots (int | None) – The number of shots for the measurement. Default:
None(which meansself.shots)with_prob (bool) – Whether to show the true probability of the measurement. Default:
Falsewires (int | list[int] | None) – The wires to measure. Default:
None(which means all wires)block_size (int) – The block size for sampling. Default: 2**24
- observable(wires: int | list[int] | None = None, basis: str = 'z') None[source]#
Add an
Observable.- Parameters:
wires (int | list[int] | None) – The wires to measure. Default:
None(which means all wires are measured)basis (str) – The measurement basis for each wire. It can be
'x','y', or'z'. If only one character is given, it is repeated for all wires. Default:'z'
- p(wires: int, inputs: Any = None, controls: int | list[int] | None = None, condition: bool = False, encode: bool = False) None[source]#
Add a phase shift gate.
- phase_damp(wires: int, inputs: Any = None, encode: bool = False) None[source]#
Add a phase-damping channel.
- phase_flip(wires: int, inputs: Any = None, encode: bool = False) None[source]#
Add a phase-flip channel.
- rbs(wires: list[int], inputs: Any = None, controls: int | list[int] | None = None, condition: bool = False, encode: bool = False) None[source]#
Add a Reconfigurable Beam Splitter gate.
- reset(wires: int | list[int] | None = None, postselect: int | None = 0) None[source]#
Add a reset operation.
- reset_circuit(init_state: Any = 'zeros') None[source]#
Reset the
QubitCircuitaccording toinit_state.
- rx(wires: int, inputs: Any = None, controls: int | list[int] | None = None, condition: bool = False, encode: bool = False) None[source]#
Add an Rx gate.
- rxlayer(wires: int | list[int] | None = None, inputs: Any = None, encode: bool = False) None[source]#
Add a layer of Rx gates.
- rxx(wires: list[int], inputs: Any = None, controls: int | list[int] | None = None, condition: bool = False, encode: bool = False) None[source]#
Add an Rxx gate.
- rxy(wires: list[int], inputs: Any = None, controls: int | list[int] | None = None, condition: bool = False, encode: bool = False) None[source]#
Add an Rxy gate.
- ry(wires: int, inputs: Any = None, controls: int | list[int] | None = None, condition: bool = False, encode: bool = False) None[source]#
Add an Ry gate.
- rylayer(wires: int | list[int] | None = None, inputs: Any = None, encode: bool = False) None[source]#
Add a layer of Ry gates.
- ryy(wires: list[int], inputs: Any = None, controls: int | list[int] | None = None, condition: bool = False, encode: bool = False) None[source]#
Add an Ryy gate.
- rz(wires: int, inputs: Any = None, controls: int | list[int] | None = None, condition: bool = False, encode: bool = False) None[source]#
Add an Rz gate.
- rzlayer(wires: int | list[int] | None = None, inputs: Any = None, encode: bool = False) None[source]#
Add a layer of Rz gates.
- rzz(wires: list[int], inputs: Any = None, controls: int | list[int] | None = None, condition: bool = False, encode: bool = False) None[source]#
Add an Rzz gate.
- s(wires: int, controls: int | list[int] | None = None, condition: bool = False) None[source]#
Add an S gate.
- sdg(wires: int, controls: int | list[int] | None = None, condition: bool = False) None[source]#
Add an S dagger gate.
- swap(wires: list[int], controls: int | list[int] | None = None, condition: bool = False) None[source]#
Add a SWAP gate.
- t(wires: int, controls: int | list[int] | None = None, condition: bool = False) None[source]#
Add a T gate.
- tdg(wires: int, controls: int | list[int] | None = None, condition: bool = False) None[source]#
Add a T dagger gate.
- transform_cut2move() QubitCircuit[source]#
Transform
WireCuttoMoveand expand the observables accordingly.
- u3(wires: int, inputs: Any = None, controls: int | list[int] | None = None, condition: bool = False, encode: bool = False) None[source]#
Add a U3 gate.
- u3layer(wires: int | list[int] | None = None, inputs: Any = None, encode: bool = False) None[source]#
Add a layer of U3 gates.
- x(wires: int, controls: int | list[int] | None = None, condition: bool = False) None[source]#
Add a Pauli-X gate.
- y(wires: int, controls: int | list[int] | None = None, condition: bool = False) None[source]#
Add a Pauli-Y gate.