tensorcircuit.results.countsΒΆ

dict related functionalities

tensorcircuit.results.counts.count2vec(count: Dict[str, int], normalization: bool = True, dim: int | None = None) Any[source]ΒΆ

Convert a dictionary of counts (with string keys) to a probability/count vector.

Support:
  • base-d string (d <= 36), characters taken from 0-9A-Z (case-insensitive) For example:

    qubit: β€˜0101’ qudit: β€˜012’ or β€˜09A’ (A represents 10, which means [0, 9, 10])

Parameters:
  • count (ct) – A dictionary mapping bit strings to counts

  • normalization (bool, optional) – Whether to normalize the counts to probabilities, defaults to True

  • dim (int, optional) – Dimensionality of the vector, defaults to 2

Returns:

Probability vector as numpy array

Return type:

Tensor

Example:

>>> count2vec({"00": 2, "10": 3, "11": 5})
array([0.2, 0. , 0.3, 0.5])
tensorcircuit.results.counts.expectation(count: Dict[str, int], z: Sequence[int] | None = None, diagonal_op: Any | None = None) float[source]ΒΆ

compute diagonal operator expectation value from bit string count dictionary

Parameters:
  • count (ct) – count dict for bitstring histogram

  • z (Optional[Sequence[int]]) – if defaults as None, then diagonal_op must be set a list of qubit that we measure Z op on

  • diagoal_op (Tensor) – shape [n, 2], explicitly indicate the diagonal op on each qubit eg. [1, -1] for z [1, 1] for I, etc.

Returns:

the expectation value

Return type:

float

tensorcircuit.results.counts.kl_divergence(c1: Dict[str, int], c2: Dict[str, int]) float[source]ΒΆ

Compute the Kullback-Leibler divergence between two count distributions.

Parameters:
  • c1 (ct) – First count dictionary

  • c2 (ct) – Second count dictionary

Returns:

KL divergence value

Return type:

float

tensorcircuit.results.counts.marginal_count(count: Dict[str, int], keep_list: Sequence[int]) Dict[str, int][source]ΒΆ

Compute the marginal distribution of a count dictionary over specified qubits.

Parameters:
  • count (ct) – A dictionary mapping bit strings to counts

  • keep_list (Sequence[int]) – List of qubit indices to keep in the marginal distribution

Returns:

A new count dictionary with marginal distribution

Return type:

ct

Example:

>>> marginal_count({"001": 10, "110": 20}, [0, 2])
{'01': 10, '10': 20}
tensorcircuit.results.counts.merge_count(*counts: Dict[str, int]) Dict[str, int][source]ΒΆ

Merge multiple count dictionaries by summing up their counts

Parameters:

counts (ct) – Variable number of count dictionaries

Returns:

Merged count dictionary

Return type:

ct

Example:

>>> merge_count({"00": 10, "01": 20}, {"00": 5, "10": 15})
{'00': 15, '01': 20, '10': 15}
tensorcircuit.results.counts.normalized_count(count: Dict[str, int]) Dict[str, float][source]ΒΆ

Normalize the count dictionary to represent probabilities.

Parameters:

count (ct) – A dictionary mapping bit strings to counts

Returns:

A new dictionary with probabilities instead of counts

Return type:

Dict[str, float]

Example:

>>> normalized_count({"00": 5, "01": 15})
{'00': 0.25, '01': 0.75}
tensorcircuit.results.counts.plot_histogram(data: Any, **kws: Any) Any[source]ΒΆ

See qiskit.visualization.plot_histogram: https://qiskit.org/documentation/stubs/qiskit.visualization.plot_histogram.html

interesting kw options include: number_to_keep (int)

Parameters:

data (Any) – _description_

Returns:

_description_

Return type:

Any

tensorcircuit.results.counts.reverse_count(count: Dict[str, int]) Dict[str, int][source]ΒΆ

Reverse the bit string keys in a count dictionary.

Parameters:

count (ct) – A dictionary mapping bit strings to counts

Returns:

A new dictionary with reversed bit string keys

Return type:

ct

Example:

>>> reverse_count({"01": 10, "10": 20})
{'10': 10, '01': 20}
tensorcircuit.results.counts.sort_count(count: Dict[str, int]) Dict[str, int][source]ΒΆ

Sort the count dictionary by counts in descending order.

Parameters:

count (ct) – A dictionary mapping bit strings to counts

Returns:

A new dictionary sorted by count values (descending)

Return type:

ct

Example:

>>> sort_count({"00": 5, "01": 15, "10": 10})
{'01': 15, '10': 10, '00': 5}
tensorcircuit.results.counts.vec2count(vec: Any, prune: bool = False, dim: int | None = None) Dict[str, int][source]ΒΆ

Map a count/probability vector of length D to a dictionary with base-d string keys (0-9A-Z). Only generate string keys when d <= 36; if d is inferred to be > 36, raise a NotImplementedError.

Parameters:
  • vec – A one-dimensional vector of length D = d**n

  • prune – Whether to prune near-zero elements (threshold 1e-8)

  • dim – Dimensionality of the vector, defaults to 2

Returns:

{base-d string key: value}, key length n