tensorcircuit.basecircuitΒΆ
Quantum circuit: common methods for all circuit classes as MixIn
Note
Supports qubit (d = 2) and qudit (d >= 2) systems.
For string-encoded samples/counts when d <= 36, digits use base-d characters 0-9A-Z (A = 10, β¦, Z = 35).
- class tensorcircuit.basecircuit.BaseCircuit[source]ΒΆ
Bases:
AbstractCircuit- amplitude(l: str | Any) Any[source]ΒΆ
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)
- Parameters:
l (Union[str, Tensor]) β The bitstring of 0 and 1s.
- Returns:
The amplitude of the circuit.
- Return type:
tn.Node.tensor
- amplitude_before(l: str | Any) List[Gate][source]ΒΆ
Returns the tensornetwor nodes for 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.
- Parameters:
l (Union[str, Tensor]) β The bitstring of 0 and 1s.
- Returns:
The tensornetwork nodes for the amplitude of the circuit.
- Return type:
List[Gate]
- append(c: AbstractCircuit, indices: List[int] | None = None) AbstractCircuitΒΆ
append circuit
cbefore- 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 β ββββββββββ
- Parameters:
c (BaseCircuit) β The other circuit to be appended
indices (Optional[List[int]], optional) β the qubit indices to which
cis appended on. Defaults to None, which means plain concatenation.
- Returns:
The composed circuit
- Return type:
- 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}]
- Parameters:
qir (List[Dict[str, Any]]) β The quantum intermediate representation.
- apply(gate: Gate | QuOperator, *index: int, name: str | None = None, split: Dict[str, Any] | None = None, mpo: bool = False, diagonal: bool = False, ir_dict: Dict[str, Any] | None = None) NoneΒΆ
An implementation of this method should also append gate directionary to self._qir
- apply_general_gate(gate: Gate | QuOperator, *index: int, name: str | None = None, split: Dict[str, Any] | None = None, mpo: bool = False, diagonal: bool = False, ir_dict: Dict[str, Any] | None = None) None[source]ΒΆ
An implementation of this method should also append gate directionary to self._qir
- static apply_general_gate_delayed(gatef: Callable[[], Gate], name: str | None = None, mpo: bool = False) Callable[[...], None]ΒΆ
- static apply_general_variable_gate_delayed(gatef: Callable[[...], Any], name: str | None = None, mpo: bool = False, diagonal: bool = False) Callable[[...], None]ΒΆ
- barrier_instruction(*index: List[int]) NoneΒΆ
add a barrier instruction flag, no effect on numerical simulation
- Parameters:
index (List[int]) β the corresponding qubits
- circuit_param: Dict[str, Any]ΒΆ
- static coloring_copied_nodes(nodes: Sequence[Node], nodes0: Sequence[Node], is_dagger: bool = True, flag: str = 'inputs') None[source]ΒΆ
Tag copied nodes while preserving the original nodeβs identity for lightcone cancellation.
- Parameters:
nodes (Sequence[tn.Node]) β A sequence of newly copied nodes.
nodes0 (Sequence[tn.Node]) β The sequence of original nodes from which nodes were copied.
is_dagger (bool, optional) β Whether the copied nodes represent conjugate operations, defaults to True.
flag (str, optional) β A label for the node type, defaults to βinputsβ.
- static coloring_nodes(nodes: Sequence[Node], is_dagger: bool = False, flag: str = 'inputs') None[source]ΒΆ
Tag nodes with metadata used for casual lightcone simplification and tracing.
- Parameters:
nodes (Sequence[tn.Node]) β A sequence of tensornetwork nodes to tag.
is_dagger (bool, optional) β Whether the nodes represent conjugate operations (U^dagger), defaults to False.
flag (str, optional) β A label for the node type (e.g., βgateβ, βinputsβ, βoperatorβ), defaults to βinputsβ.
- cond_measure(index: int, status: float | None = None) AnyΒΆ
Measurement on z basis at
indexqubit 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)
Note
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- Parameters:
index (int) β the site index for the Z-basis measurement
- Returns:
0 or 1 for Z-basis measurement outcome
- Return type:
Tensor
- cond_measurement(index: int, status: float | None = None) Any[source]ΒΆ
Measurement on z basis at
indexqubit 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)
Note
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- Parameters:
index (int) β the site index for the Z-basis measurement
- Returns:
0 or 1 for Z-basis measurement outcome
- Return type:
Tensor
- conditional_gate(which: Any, kraus: Sequence[Gate], *index: int) NoneΒΆ
Apply
which-th gate fromkrauslist, i.e. apply kraus[which]- Parameters:
which (Tensor) β Tensor of shape [] and dtype int
kraus (Sequence[Gate]) β A list of gate in the form of
tc.gateor Tensorindex (int) β the qubit lines the gate applied on
- copy() AbstractCircuitΒΆ
- static copy_nodes(nodes: Sequence[Node], dangling: Sequence[Edge] | None = None, conj: bool | None = False) Tuple[List[Node], List[Edge]][source]ΒΆ
copy all nodes and dangling edges correspondingly
- Returns:
- diaggates = ['diagonal', 'rzm', 'cmz']ΒΆ
- 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 ββββββ βββββ
- expectation(*ops: Tuple[Node, List[int]], reuse: bool = True, noise_conf: Any | None = None, nmc: int = 1000, status: Any | None = None, **kws: Any) AnyΒΆ
- expectation_before(*ops: Tuple[Node, List[int]], reuse: bool = True, **kws: Any) List[Node][source]ΒΆ
Get the tensor network in the form of a list of nodes for the expectation calculation before the real contraction
- Parameters:
reuse (bool, optional) β _description_, defaults to True
- Raises:
ValueError β _description_
- Returns:
_description_
- Return type:
List[tn.Node]
- expectation_ps(x: Sequence[int] | None = None, y: Sequence[int] | None = None, z: Sequence[int] | None = None, ps: Sequence[int] | None = None, reuse: bool = True, noise_conf: Any | None = None, nmc: int = 1000, status: Any | None = 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)
- Parameters:
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,pscan overwritex,yandzreuse (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
- Returns:
Expectation value
- Return type:
Tensor
- classmethod from_cirq(qc: Any, n: int | None = None, inputs: List[float] | None = None, circuit_params: Dict[str, Any] | None = None) AbstractCircuitΒΆ
Import Cirq Circuit object as a
tc.Circuitobject.- Example:
>>> import cirq >>> c = cirq.Circuit() >>> q = cirq.LineQubit.range(3) >>> c.append(cirq.H(q[0])) >>> c.append(cirq.CNOT(q[0], q[1])) >>> tc_c = tc.Circuit.from_cirq(c)
- Parameters:
qc (cirq.Circuit) β Cirq Circuit object
n (int) β The number of qubits for the circuit
inputs (Optional[List[float]], optional) β possible input wavefunction for
tc.Circuit, defaults to Nonecircuit_params (Optional[Dict[str, Any]]) β kwargs given in Circuit.__init__ construction function, default to None.
- Returns:
The same circuit but as tensorcircuit object
- Return type:
- classmethod from_json(jsonstr: str, circuit_params: Dict[str, Any] | None = None) AbstractCircuitΒΆ
load json str as a Circuit
- Parameters:
jsonstr (str) β _description_
circuit_params (Optional[Dict[str, Any]], optional) β Extra circuit parameters in the format of
__init__, defaults to None
- Returns:
_description_
- Return type:
- classmethod from_json_file(file: str, circuit_params: Dict[str, Any] | None = None) AbstractCircuitΒΆ
load json file and convert it to a circuit
- Parameters:
file (str) β filename
circuit_params (Optional[Dict[str, Any]], optional) β _description_, defaults to None
- Returns:
_description_
- Return type:
- classmethod from_openqasm(qasmstr: str, circuit_params: Dict[str, Any] | None = None, keep_measure_order: bool = False) AbstractCircuitΒΆ
- classmethod from_openqasm_file(file: str, circuit_params: Dict[str, Any] | None = None, keep_measure_order: bool = False) AbstractCircuitΒΆ
- classmethod from_qir(qir: List[Dict[str, Any]], circuit_params: Dict[str, Any] | None = None) 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)
- Parameters:
qir (List[Dict[str, Any]]) β The quantum intermediate representation of a circuit.
circuit_params (Optional[Dict[str, Any]]) β Extra circuit parameters.
- Returns:
The circuit have same gates in the qir.
- Return type:
- classmethod from_qiskit(qc: Any, n: int | None = None, inputs: List[float] | None = None, circuit_params: Dict[str, Any] | None = None, binding_params: Sequence[float] | Dict[Any, float] | None = None) AbstractCircuitΒΆ
Import Qiskit QuantumCircuit object as a
tc.Circuitobject.- 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)
- Parameters:
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 Nonecircuit_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
ParameterVectorElementuse sequence. ForParameteruse dictionary
- Returns:
The same circuit but as tensorcircuit object
- Return type:
- classmethod from_qsim_file(file: str, circuit_params: Dict[str, Any] | None = None) AbstractCircuitΒΆ
- gate_aliases = [['cnot', 'cx'], ['fredkin', 'cswap'], ['toffoli', 'ccnot'], ['toffoli', 'ccx'], ['any', 'unitary'], ['sd', 'sdg'], ['td', 'tdg']]ΒΆ
- gate_count(gate_list: str | Sequence[str] | None = 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
- Parameters:
gate_list (Optional[Sequence[str]], optional) β gate name or gate name list to be counted, defaults to None (counting all gates)
- Returns:
the total number of all gates or gates in the
gate_list- Return type:
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
- Parameters:
cond_func (Callable[[Dict[str, Any]], bool]) β the condition for counting the gate
- Returns:
the total number of all gates which satisfy the
condition- Return type:
int
- gate_summary() Dict[str, int]ΒΆ
return the summary dictionary on gate type - gate count pair
- Returns:
the gate count dict by gate type
- Return type:
Dict[str, int]
- 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
- Returns:
positional_logical_mapping- Return type:
Dict[int, int]
- get_quvector() QuVector[source]ΒΆ
Get the representation of the output state in the form of
QuVectorwhile maintaining the circuit uncomputed- Returns:
QuVectorrepresentation of the output state from the circuit- Return type:
- initial_mapping(logical_physical_mapping: Dict[int, int], n: int | None = None, circuit_params: Dict[str, Any] | None = None) AbstractCircuitΒΆ
generate a new circuit with the qubit mapping given by
logical_physical_mapping- Parameters:
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
- Returns:
_description_
- Return type:
- inputs: AnyΒΆ
- inverse(circuit_params: Dict[str, Any] | None = None) 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()
- Parameters:
circuit_params (Optional[Dict[str, Any]], optional) β keywords dict for initialization the new circuit, defaults to None
- Returns:
the inversed circuit
- Return type:
- is_dm: boolΒΆ
- is_mps: bool = FalseΒΆ
- measure(*index: int, with_prob: bool = False, status: Any | None = None) Tuple[Any, Any]ΒΆ
Take measurement on the given site indices (computational basis). This method is jittable is and about 100 times faster than unjit version!
- Parameters:
index (int) β Measure on which site (wire) index.
with_prob (bool, optional) β If true, theoretical probability is also returned.
status (Optional[Tensor]) β external randomness, with shape [index], defaults to None
- Returns:
The sample output and probability (optional) of the quantum line.
- Return type:
Tuple[Tensor, Tensor]
- measure_instruction(*index: int) NoneΒΆ
add a measurement instruction flag, no effect on numerical simulation
- Parameters:
index (int) β the corresponding qubits
- measure_jit(*index: int, with_prob: bool = False, status: Any | None = None) Tuple[Any, Any][source]ΒΆ
Take measurement on the given site indices (computational basis). This method is jittable is and about 100 times faster than unjit version!
- Parameters:
index (int) β Measure on which site (wire) index.
with_prob (bool, optional) β If true, theoretical probability is also returned.
status (Optional[Tensor]) β external randomness, with shape [index], defaults to None
- Returns:
The sample output and probability (optional) of the quantum line.
- Return type:
Tuple[Tensor, Tensor]
- mpogates = ['multicontrol', 'mpo']ΒΆ
- perfect_sampling(status: Any | None = None) Tuple[str, float][source]ΒΆ
Sampling base-d strings (0-9A-Z when d <= 36) from the circuit output based on quantum amplitudes. Reference: arXiv:1201.3974.
- Parameters:
status (Optional[Tensor]) β external randomness, with shape [nqubits], defaults to None
- Returns:
Sampled base-d string and the corresponding theoretical probability.
- Return type:
Tuple[str, float]
- prepend(c: AbstractCircuit) AbstractCircuitΒΆ
prepend circuit
cbefore- Parameters:
c (BaseCircuit) β The other circuit to be prepended
- Returns:
The composed circuit
- Return type:
- probability() Any[source]ΒΆ
get the d^n length probability vector over computational basis
- Returns:
probability vector of shape [dim**n]
- Return type:
Tensor
- projected_subsystem(traceout: Any, left: Tuple[int, ...]) Any[source]ΒΆ
remaining wavefunction or density matrix on sites in
left, with other sites fixed to given digits (0..d-1) as indicated bytraceout- Parameters:
traceout (Tensor) β can be jitted
left (Tuple) β cannot be jitted
- Returns:
_description_
- Return type:
Tensor
- quvector() QuVectorΒΆ
Get the representation of the output state in the form of
QuVectorwhile maintaining the circuit uncomputed- Returns:
QuVectorrepresentation of the output state from the circuit- Return type:
- readouterror_bs(readout_error: Sequence[Any] | None = None, p: Any | None = None) Any[source]ΒΆ
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]
- Parameters:
readout_error (Optional[Sequence[Any]]. Tensor, List, Tuple) β list of readout error for each qubits.
p (Optional[Any]) β probabilities of bit string
- Return type:
Tensor
- replace_inputs(inputs: Any) None[source]ΒΆ
Replace the input state with the circuit structure unchanged.
- Parameters:
inputs (Tensor) β Input wavefunction.
- reset_instruction(*index: int) NoneΒΆ
add a reset instruction flag, no effect on numerical simulation
- Parameters:
index (int) β the corresponding qubits
- sample(batch: int | None = None, allow_state: bool = False, readout_error: Sequence[Any] | None = None, format: str | None = None, random_generator: Any | None = None, status: Any | None = None, jittable: bool = True) Any[source]ΒΆ
batched sampling from state or circuit tensor network directly
- Parameters:
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()Six formats of measurement counts results:βsample_intβ: # np.array([0, 0])
βsample_binβ: # [np.array([1, 0]), np.array([1, 0])]
βcount_vectorβ: # np.array([2, 0, 0, 0])
βcount_tupleβ: # (np.array([0]), np.array([2]))
- βcount_dict_binβ: # {β00β: 2, β01β: 0, β10β: 0, β11β: 0}
for cases din [11, 36], use 0-9A-Z digits (e.g., βAβ -> 10, β¦, βZβ -> 35);
βcount_dict_intβ: # {0: 2, 1: 0, 2: 0, 3: 0}
format β alias for the argument
formatrandom_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, shape [batch] for allow_state=True and shape [batch, nqubits] for allow_state=False using perfect sampling implementation
jittable (bool, defaults true) β when converting to count, whether keep the full size. if false, may be conflict external jit, if true, may fail for large scale system with actual limited count results
- Returns:
List (if batch) of tuple (binary configuration tensor and corresponding probability) if the format is None, and consistent with format when given
- Return type:
Any
- sample_expectation_ps(x: Sequence[int] | None = None, y: Sequence[int] | None = None, z: Sequence[int] | None = None, shots: int | None = None, random_generator: Any | None = None, status: Any | None = None, readout_error: Sequence[Any] | None = None, noise_conf: Any | None = None, nmc: int = 1000, statusc: Any | None = None, **kws: Any) Any[source]ΒΆ
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
- Parameters:
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
- Returns:
[description]
- Return type:
Tensor
- select_gate(which: Any, kraus: Sequence[Gate], *index: int) NoneΒΆ
Apply
which-th gate fromkrauslist, i.e. apply kraus[which]- Parameters:
which (Tensor) β Tensor of shape [] and dtype int
kraus (Sequence[Gate]) β A list of gate in the form of
tc.gateor Tensorindex (int) β the qubit lines the gate applied on
- sexpps(x: Sequence[int] | None = None, y: Sequence[int] | None = None, z: Sequence[int] | None = None, shots: int | None = None, random_generator: Any | None = None, status: Any | None = None, readout_error: Sequence[Any] | None = None, noise_conf: Any | None = None, nmc: int = 1000, statusc: Any | None = 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
- Parameters:
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
- Returns:
[description]
- Return type:
Tensor
- sgates = ['i', 'x', 'y', 'z', 'h', 't', 's', 'td', 'sd', 'wroot', 'cnot', 'cz', 'swap', 'cy', 'ox', 'oy', 'oz', 'toffoli', 'fredkin']ΒΆ
- split: Dict[str, Any] | NoneΒΆ
- static standardize_gate(name: str) strΒΆ
standardize the gate name to tc common gate sets
- Parameters:
name (str) β non-standard gate name
- Returns:
the standard gate name
- Return type:
str
- tex(**kws: Any) strΒΆ
Generate latex string based on quantikz latex package
- Returns:
Latex string that can be directly compiled via, e.g. latexit
- Return type:
str
- to_cirq(enable_instruction: bool = False) AnyΒΆ
Translate
tc.Circuitto a cirq circuit object.- Parameters:
enable_instruction (bool, defaults to False) β whether also export measurement and reset instructions
- Returns:
A cirq circuit of this circuit.
- to_graphviz(graph: Graph | None = None, include_all_names: bool = False, engine: str = 'neato') Graph[source]ΒΆ
Not an ideal visualization for quantum circuit, but reserve here as a general approach to show the tensornetwork [Deprecated, use
Circuit.vis_texorCircuit.drawinstead]
- to_json(file: str | None = None, simplified: bool = False) AnyΒΆ
circuit dumps to json
- Parameters:
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
- Returns:
None if dumps to file otherwise the json str
- Return type:
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- Returns:
circuit representation in openqasm format
- Return type:
str
- to_openqasm_file(file: str, **kws: Any) NoneΒΆ
save the circuit to openqasm file
- Parameters:
file (str) β the file path to save the circuit
- 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}]
- Returns:
The quantum intermediate representation of the circuit.
- Return type:
List[Dict[str, Any]]
- to_qiskit(enable_instruction: bool = False, enable_inputs: bool = False) AnyΒΆ
Translate
tc.Circuitto a qiskit QuantumCircuit object.- Parameters:
enable_instruction (bool, defaults to False) β whether also export measurement and reset instructions
enable_inputs (bool, defaults to False) β whether also export the inputs
- Returns:
A qiskit object of this circuit.
- vgates = ['r', 'cr', 'u', 'cu', 'rx', 'ry', 'rz', 'phase', 'rxx', 'ryy', 'rzz', 'cphase', 'crx', 'cry', 'crz', 'orx', 'ory', 'orz', 'iswap', 'any', 'exp', 'exp1', 'su4']ΒΆ
- vis_tex(**kws: Any) strΒΆ
Generate latex string based on quantikz latex package
- Returns:
Latex string that can be directly compiled via, e.g. latexit
- Return type:
str