1919from collections import defaultdict
2020from dataclasses import dataclass
2121from random import Random
22- from typing import Dict , FrozenSet , Iterable , Optional , Tuple , Union
22+ from typing import Any , Dict , FrozenSet , Iterable , Optional , Tuple , Union
2323
2424import autoray as ar
2525import more_itertools as mit
3737def is_classical_operation (m : Matrix ) -> bool :
3838 """Check if given matrix is a classical operation.
3939
40- Check if given matrix is a classical operation. An operation is classical
41- if 'm @ v' only permutes the elements of 'v' (excluding a change of phase).
40+ An operation is classical if ``m @ v`` only permutes the elements of ``v``
41+ (excluding a change of phase).
4242
4343 Args:
4444 m: Matrix to check.
4545
4646 Returns:
47- True if 'm' is a classical operation.
47+ `` True`` if ``m`` is a classical operation.
4848 """
4949 m = ar .do ('asarray' , m )
5050 if m .ndim != 2 or m .shape [0 ] != m .shape [1 ] and int (math .log2 (
@@ -104,56 +104,54 @@ def sample(
104104 qubit_order : Optional [Iterable [Qubit ]] = None ,
105105 normalize : Optional [bool ] = True ,
106106 return_intermediate_state_only : Optional [bool ] = False ,
107- dtype : Optional [any ] = None ,
107+ dtype : Optional [Any ] = None ,
108108 optimization_backend : Optional [str ] = None ,
109109 contraction_backend : Optional [str ] = None ,
110110 seed : Optional [int ] = None ,
111111 verbose : Optional [int ] = False ,
112112 ** optimize_params
113113) -> Union [Tuple [Dict [str , int ], Tuple [Qubit ]], SamplingIntermediateState ]:
114- """Sample bitstring from a circuit.
114+ """Sample bitstrings from a circuit.
115115
116- Sample bitstring from a circuit using the Bravyi-Gosset-Liu algorithm
116+ Sample bitstrings from a circuit using the Bravyi-Gosset-Liu algorithm
117117 presented in "How to Simulate Quantum Measurement without Computing
118118 Marginals", Phys. Rev. Lett. 128, 220503 (2022).
119119
120120 Args:
121- circuit: The circuit to sample from. If a ' SamplingIntermediateState'
122- is passed insted , the optimization phase of the circuit is skipped
121+ circuit: The circuit to sample from. If a `` SamplingIntermediateState``
122+ is passed instead , the optimization phase of the circuit is skipped
123123 and the intermediate state is used to sample.
124- optimizer: The ' Optimizer' to use for the tensor network optimization.
124+ optimizer: The `` Optimizer`` to use for the tensor network optimization.
125125 n_samples: The number of total samples to collect.
126- simplify: If 'True', gates that cancel each other will be simplified.
127- use_matrix_commutation: If 'True', gates will be commuted by performing
128- the actual matrix commutation while simplifying the circuit.
129- decompose_hyper_inds: If 'True', decompose gates to get hyper-indices.
130- fuse: Fuse tensors together up a width smaller than 'fuse'. The width
126+ simplify: If ``True``, gates that cancel each other will be simplified.
127+ use_matrix_commutation: If ``True``, gates will be commuted by
128+ performing the actual matrix commutation while simplifying the
129+ circuit.
130+ decompose_hyper_inds: If ``True``, decompose gates to get hyper-indices.
131+ fuse: Fuse tensors together up a width smaller than ``fuse``. The width
131132 is defined as sum of the logarithms of all the dimensions of a
132133 given tensor.
133134 qubit_order: If provided, the order of qubits to use.
134- normalize: If True, the total number of hits are divided by the number
135- of samples.
136- load_params: The parameters to pass to 'tnco.utils.circuit.load' to
137- convert circuit to a tensor.
138- optimizer_params: The parameters to pass to 'tnco.app.Optimizer' to
139- initialize a new optimizer.
135+ normalize: If ``True``, the total number of hits are divided by the
136+ number of samples.
140137 optimize_params: The parameters to pass to
141- 'tnco.app.Optimizer.optimize' to optimize each partial contraction.
142- return_intermediate_state_only: If True, skip the sampling and return
143- the 'SamplingIntermediateState'.
138+ ``tnco.app.Optimizer.optimize`` to optimize each partial
139+ contraction.
140+ return_intermediate_state_only: If ``True``, skip the sampling and
141+ return the ``SamplingIntermediateState``.
144142 dtype: The type to use for the contraction.
145143 optimization_backend: The backend to use to manipulate arrays while
146- loading and optimazing the tensor network.
144+ loading and optimizing the tensor network.
147145 contraction_backend: The backend to use for the contraction.
148146 seed: The seed to use for the sampling.
149147 verbose: Verbose output.
150148
151149 Returns:
152- If ' return_intermediate_state_only' , a ' SamplingIntermediateState' is
153- return to be reused multiple times. Othewise , it return a dictionary
154- with the fraction of hits for each bitstring sampled, and the order of
155- qubits. If ' normalize=False' , the the number of hits is returned
156- instead of its fraction.
150+ If `` return_intermediate_state_only`` , a `` SamplingIntermediateState``
151+ is returned to be reused multiple times. Otherwise , it returns a
152+ dictionary with the fraction of hits for each bitstring sampled, and
153+ the order of qubits. If `` normalize=False`` , the number of hits is
154+ returned instead of its fraction.
157155 """
158156
159157 # Short for asarray
@@ -416,42 +414,30 @@ def _(circuit: qiskit.QuantumCircuit, *args, **kwargs):
416414
417415@dataclass (frozen = True )
418416class Sampler :
419- """Sample bitstrings from circuit.
417+ """Sample bitstrings from a circuit.
420418
421- Sample bitstring from a circuit using the Bravyi-Gosset-Liu algorithm
419+ Sample bitstrings from a circuit using the Bravyi-Gosset-Liu algorithm
422420 presented in "How to Simulate Quantum Measurement without Computing
423421 Marginals", Phys. Rev. Lett. 128, 220503 (2022).
424422
425423 Args:
426424 max_width: Maximum width to use. The width is defined as sum of the
427- logarithms of all the dimensions of a given tensor. Tensors are
425+ logarithms of all the dimensions of a given tensor. Tensors are
428426 contracted so that the width of the contracted tensor is smaller
429- than ' max_width' .
427+ than `` max_width`` .
430428 n_jobs: Number of processes to use. By default, all available cores are
431- used. If ' n_jobs' is a positive number, ' n_jobs' processes will be
432- used. If ' n_jobs' is negative, ' n_cpus + n_jobs + 1' will be used.
433- If ' n_jobs' is zero, it will raise a ' ValueError' . (See:
434- ' tnco.parallel.Parallel' )
429+ used. If `` n_jobs`` is a positive number, `` n_jobs`` processes will
430+ be used. If `` n_jobs`` is negative, `` n_cpus + n_jobs + 1`` will be
431+ used. If `` n_jobs`` is zero, it will raise a `` ValueError`` . (See:
432+ `` tnco.parallel.Parallel`` )
435433 width_type: The type to use to represent the width. (See:
436- ' tnco.optimize.finite_width.cost_model.SimpleCostModel' )
434+ `` tnco.optimize.finite_width.cost_model.SimpleCostModel`` )
437435 cost_type: The type to use to represent the cost. (See:
438- 'tnco.optimize.finite_width.cost_model.SimpleCostModel')
439- output_format: Format to use for the output. See Notes for more
440- details.
441- output_filename: If provided, dump the output to 'output_filename'. If
442- 'output_filfename' has a '.gzip' or '.bz2' extension, it will be
443- compressed accordingly.
444- output_compression: If 'auto', the output will be compressed to the
445- format specified by 'output_compression'. Otherwise, the
446- compression format will be deduced from 'output_filename'. Valid
447- 'output_compression' are 'none', 'bz2' and 'json'. If
448- 'output_filename' is not provided, it will raise a 'ValueError'.
449- overwrite_output_file: If 'True', the 'output_filename' will be
450- overwritten if it exists.
451- atol: Absolute tollerance when checking for hyper-indices.
436+ ``tnco.optimize.finite_width.cost_model.SimpleCostModel``)
437+ atol: Absolute tolerance when checking for hyper-indices.
452438 dtype: The type to use for the arrays.
453439 optimization_backend: The backend to use to manipulate arrays while
454- loading and optimazing the tensor network.
440+ loading and optimizing the tensor network.
455441 seed: Seed to use.
456442 verbose: Verbose output.
457443 """
@@ -460,7 +446,7 @@ class Sampler:
460446 width_type : Optional [str ] = 'float32'
461447 cost_type : Optional [str ] = 'float64'
462448 atol : Optional [float ] = 1e-5
463- dtype : Optional [any ] = None
449+ dtype : Optional [Any ] = None
464450 optimization_backend : Optional [str ] = None
465451 seed : Optional [int ] = None
466452 verbose : Optional [int ] = False
@@ -501,48 +487,43 @@ def sample(
501487 contraction_backend : Optional [str ] = None ,
502488 ** optimize_params
503489 ) -> Union [Tuple [Dict [str , int ], Tuple [Qubit ]], SamplingIntermediateState ]:
504- """Sample bitstring from a circuit.
490+ """Sample bitstrings from a circuit.
505491
506- Sample bitstring from a circuit using the Bravyi-Gosset-Liu algorithm
492+ Sample bitstrings from a circuit using the Bravyi-Gosset-Liu algorithm
507493 presented in "How to Simulate Quantum Measurement without Computing
508494 Marginals", Phys. Rev. Lett. 128, 220503 (2022).
509495
510496 Args:
511- circuit: Circuit to sample from. If a ' SamplingIntermediateState'
512- is passed insted , the optimization phase of the circuit is
497+ circuit: Circuit to sample from. If a `` SamplingIntermediateState``
498+ is passed instead , the optimization phase of the circuit is
513499 skipped and the intermediate state is used to sample.
514500 n_samples: The number of total samples to collect.
515- simplify: If ' True' , gates that cancel each other will be
501+ simplify: If `` True`` , gates that cancel each other will be
516502 simplified.
517- use_matrix_commutation: If ' True' , gates will be commuted by
503+ use_matrix_commutation: If `` True`` , gates will be commuted by
518504 performing the actual matrix commutation while simplifying the
519505 circuit.
520- decompose_hyper_inds: If ' True' , decompose gates to get
506+ decompose_hyper_inds: If `` True`` , decompose gates to get
521507 hyper-indices.
522- fuse: Fuse tensors together up a width smaller than ' fuse' . The
508+ fuse: Fuse tensors together up a width smaller than `` fuse`` . The
523509 width is defined as sum of the logarithms of all the dimensions
524510 of a given tensor.
525511 qubit_order: If provided, the order of qubits to use.
526- normalize: If True, the total number of hits are divided by the
512+ normalize: If `` True`` , the total number of hits are divided by the
527513 number of samples.
528- load_params: Parameters to pass to 'tnco.utils.circuit.load' to
529- convert circuit to a tensor.
530- optimizer_params: Parameters to pass to 'tnco.app.Optimizer' to
531- initialize a new optimizer.
532- optimize_params: Parameters to pass to 'tnco.app.Optimizer.optimize'
533- to optimize each partial contraction.
534- return_intermediate_state_only: If True, skip the sampling and
535- return the 'SamplingIntermediateState'.
514+ return_intermediate_state_only: If ``True``, skip the sampling and
515+ return the ``SamplingIntermediateState``.
536516 contraction_backend: The backend to use for the contraction.
537517 **optimize_params: Parameters to use to optimize the tensor network
538- contraction. (See ' tnco.app.Optimizer.optimize' )
518+ contraction. (See `` tnco.app.Optimizer.optimize`` )
539519
540520 Returns:
541- If 'return_intermediate_state_only', a 'SamplingIntermediateState'
542- is return to be reused multiple times. Othewise, it return a
543- dictionary with the fraction of hits for each bitstring sampled,
544- and the order of qubits. If 'normalize=False', the the number of
545- hits is returned instead of its fraction.
521+ If ``return_intermediate_state_only``, a
522+ ``SamplingIntermediateState`` is returned to be reused multiple
523+ times. Otherwise, it returns a dictionary with the fraction of hits
524+ for each bitstring sampled, and the order of qubits. If
525+ ``normalize=False``, the number of hits is returned instead of its
526+ fraction.
546527 """
547528
548529 return sample (
0 commit comments