quimb.tensor.circuit.exact ========================== .. py:module:: quimb.tensor.circuit.exact .. autoapi-nested-parse:: Exact tensor-network circuit simulators (``Circuit``, ``CircuitDense``). Classes ------- .. autoapisummary:: quimb.tensor.circuit.exact.Circuit quimb.tensor.circuit.exact.CircuitDense Module Contents --------------- .. py:class:: Circuit(N=None, psi0=None, gate_opts=None, gate_contract='auto-split-gate', gate_propagate_tags='register', tags=None, psi0_dtype='complex128', psi0_tag='PSI0', tag_gate_numbers=True, gate_tag_id='GATE_{}', tag_gate_rounds=True, round_tag_id='ROUND_{}', tag_gate_labels=True, bra_site_ind_id='b{}', dtype=None, to_backend=None, convert_eager=False) Bases: :py:obj:`quimb.tensor.circuit.core.CircuitBase` Class for simulating quantum circuits using tensor networks. The class keeps a list of :class:`Gate` objects in sync with a tensor network representing the current state of the circuit. :param N: The number of qubits. :type N: int, optional :param psi0: The initial state, assumed to be ``|00000....0>`` if not given. The state is always copied and the tag ``PSI0`` added. :type psi0: TensorNetwork1DVector, optional :param gate_opts: Default keyword arguments to supply to each :func:`~quimb.tensor.tn1d.core.gate_TN_1D` call during the circuit. :type gate_opts: dict_like, optional :param gate_contract: Shortcut for setting the default `'contract'` option in `gate_opts`. :type gate_contract: str, optional :param gate_propagate_tags: Shortcut for setting the default `'propagate_tags'` option in `gate_opts`. :type gate_propagate_tags: str, optional :param tags: Tag(s) to add to the initial wavefunction tensors (whether these are propagated to the rest of the circuit's tensors depends on ``gate_opts``). :type tags: str or sequence of str, optional :param psi0_dtype: Ensure the initial state has this dtype. :type psi0_dtype: str, optional :param psi0_tag: Ensure the initial state has this tag. :type psi0_tag: str, optional :param tag_gate_numbers: Whether to tag each gate tensor with its number in the circuit, like ``"GATE_{g}"``. This is required for updating the circuit parameters. :type tag_gate_numbers: bool, optional :param gate_tag_id: The format string for tagging each gate tensor, by default e.g. ``"GATE_{g}"``. :type gate_tag_id: str, optional :param tag_gate_rounds: Whether to tag each gate tensor with its number in the circuit, like ``"ROUND_{r}"``. :type tag_gate_rounds: bool, optional :param round_tag_id: The format string for tagging each round of gates, by default e.g. ``"ROUND_{r}"``. :type round_tag_id: str, optional :param tag_gate_labels: Whether to tag each gate tensor with its gate type label, e.g. ``{"X_1/2", "ISWAP", "CCX", ...}``.. :type tag_gate_labels: bool, optional :param bra_site_ind_id: Use this to label 'bra' site indices when creating certain (mostly internal) intermediate tensor networks. :type bra_site_ind_id: str, optional :param dtype: A default dtype to perform calculations in. Depending on `convert_eager`, this is enforced *after* circuit construction and simplification (the default for exact simulation), or eagerly to the initial state and as gates are applied (the default for MPS simulation). :type dtype: str, optional :param to_backend: If given, apply this function to both the initial state arrays and to every gate as it is applied. :type to_backend: callable, optional :param convert_eager: Whether to eagerly perform dtype casting and application of `to_backend` as gates are supplied, or wait until after the necessary TNs for a particular task such as sampling are formed and simplified. Deferred conversion (`convert_eager=False`) is the default mode for full contraction. :type convert_eager: bool, optional .. attribute:: psi The current circuit wavefunction as a tensor network. :type: TensorNetwork1DVector .. attribute:: uni The current circuit unitary operator as a tensor network. :type: TensorNetwork1DOperator .. attribute:: gates The gates in the circuit. :type: tuple[Gate] .. rubric:: Examples Create 3-qubit GHZ-state: >>> qc = qtn.Circuit(3) >>> gates = [ ('H', 0), ('H', 1), ('CNOT', 1, 2), ('CNOT', 0, 2), ('H', 0), ('H', 1), ('H', 2), ] >>> qc.apply_gates(gates) >>> qc.psi >>> qc.psi.to_dense().round(4) qarray([[ 0.7071+0.j], [ 0. +0.j], [ 0. +0.j], [-0. +0.j], [-0. +0.j], [ 0. +0.j], [ 0. +0.j], [ 0.7071+0.j]]) >>> for b in qc.sample(10): ... print(b) 000 000 111 000 111 111 000 111 000 000 .. seealso:: :py:obj:`Gate` .. py:method:: _init_state(N, dtype='complex128') .. py:method:: get_psi() Get a copy of the current state tensor network, with any singlet dimensions squeezed out. .. py:method:: get_uni(transposed=False) Tensor network representation of the unitary operator (i.e. with the initial state removed). .. py:property:: uni Tensor network representation of the unitary operator, i.e. the circuit with the initial state removed, such that ``circ.uni.to_dense()`` gives ``U`` acting on a state like ``U @ psi``. For the old transposed convention use ``circ.get_uni(transposed=True)``. .. py:method:: get_reverse_lightcone_tags(where) Get the tags of gates in this circuit corresponding to the 'reverse' lightcone propagating backwards from registers in ``where``. :param where: The register or register to get the reverse lightcone of. :type where: int or sequence of int :returns: The sequence of gate tags (``GATE_{i}``, ...) corresponding to the lightcone. :rtype: tuple[str] .. py:method:: get_psi_reverse_lightcone(where, keep_psi0=False) Get just the bit of the wavefunction in the reverse lightcone of sites in ``where`` - i.e. causally linked. :param where: The sites to propagate the the lightcone back from, supplied to :meth:`~quimb.tensor.circuit.Circuit.get_reverse_lightcone_tags`. :type where: int, or sequence of int :param keep_psi0: Keep the tensors corresponding to the initial wavefunction regardless of whether they are outside of the lightcone. :type keep_psi0: bool, optional :returns: **psi_lc** :rtype: TensorNetwork1DVector .. py:method:: get_psi_simplified(seq='ADCRS', atol=1e-12, equalize_norms=False) Get the full wavefunction post local tensor network simplification. :param seq: Which local tensor network simplifications to perform and in which order, see :meth:`~quimb.tensor.tensor_core.TensorNetwork.full_simplify`. :type seq: str, optional :param atol: The tolerance with which to compare to zero when applying :meth:`~quimb.tensor.tensor_core.TensorNetwork.full_simplify`. :type atol: float, optional :param equalize_norms: Actively renormalize tensor norms during simplification. :type equalize_norms: bool, optional :returns: **psi** :rtype: TensorNetwork1DVector .. py:method:: get_rdm_lightcone_simplified(where, seq='ADCRS', atol=1e-12, equalize_norms=False) Get a simplified TN of the norm of the wavefunction, with gates outside reverse lightcone of ``where`` cancelled, and physical indices within ``where`` preserved so that they can be fixed (sliced) or used as output indices. :param where: The region assumed to be the target density matrix essentially. Supplied to :meth:`~quimb.tensor.circuit.Circuit.get_reverse_lightcone_tags`. :type where: int or sequence of int :param seq: Which local tensor network simplifications to perform and in which order, see :meth:`~quimb.tensor.tensor_core.TensorNetwork.full_simplify`. :type seq: str, optional :param atol: The tolerance with which to compare to zero when applying :meth:`~quimb.tensor.tensor_core.TensorNetwork.full_simplify`. :type atol: float, optional :param equalize_norms: Actively renormalize tensor norms during simplification. :type equalize_norms: bool, optional :rtype: TensorNetwork .. py:method:: amplitude(b, optimize='auto-hq', simplify_sequence='ADCRS', simplify_atol=1e-12, simplify_equalize_norms=True, backend=None, dtype=None, rehearse=False) Get the amplitude coefficient of bitstring ``b``. .. math:: c_b = \langle b | \psi \rangle :param b: The bitstring to compute the transition amplitude for. :type b: str or sequence of int :param optimize: Contraction path optimizer to use for the amplitude, can be a non-reusable path optimizer as only called once (though path won't be cached for later use in that case). :type optimize: str, optional :param simplify_sequence: Which local tensor network simplifications to perform and in which order, see :meth:`~quimb.tensor.tensor_core.TensorNetwork.full_simplify`. :type simplify_sequence: str, optional :param simplify_atol: The tolerance with which to compare to zero when applying :meth:`~quimb.tensor.tensor_core.TensorNetwork.full_simplify`. :type simplify_atol: float, optional :param simplify_equalize_norms: Actively renormalize tensor norms during simplification. :type simplify_equalize_norms: bool, optional :param backend: Backend to perform the contraction with, e.g. ``'numpy'``, ``'cupy'`` or ``'jax'``. Passed to ``cotengra``. :type backend: str, optional :param dtype: Data type to cast the TN to before contraction. :type dtype: str, optional :param rehearse: If ``True``, generate and cache the simplified tensor network and contraction tree but don't actually perform the contraction. Returns a dict with keys ``"tn"`` and ``'tree'`` with the tensor network that will be contracted and the corresponding contraction tree if so. :type rehearse: bool or "tn", optional .. py:method:: amplitude_rehearse(b='random', simplify_sequence='ADCRS', simplify_atol=1e-12, simplify_equalize_norms=True, optimize='auto-hq', dtype=None, rehearse=True) Perform just the tensor network simplifications and contraction tree finding associated with computing a single amplitude (caching the results) but don't perform the actual contraction. :param b: The bitstring to rehearse computing the transition amplitude for, if ``'random'`` (the default) a random bitstring will be used. :type b: 'random', str or sequence of int :param optimize: Contraction path optimizer to use for the marginal, can be a non-reusable path optimizer as only called once (though path won't be cached for later use in that case). :type optimize: str, optional :param simplify_sequence: Which local tensor network simplifications to perform and in which order, see :meth:`~quimb.tensor.tensor_core.TensorNetwork.full_simplify`. :type simplify_sequence: str, optional :param simplify_atol: The tolerance with which to compare to zero when applying :meth:`~quimb.tensor.tensor_core.TensorNetwork.full_simplify`. :type simplify_atol: float, optional :param simplify_equalize_norms: Actively renormalize tensor norms during simplification. :type simplify_equalize_norms: bool, optional :param backend: Backend to perform the marginal contraction with, e.g. ``'numpy'``, ``'cupy'`` or ``'jax'``. Passed to ``cotengra``. :type backend: str, optional :param dtype: Data type to cast the TN to before contraction. :type dtype: str, optional :rtype: dict .. py:attribute:: amplitude_tn .. py:method:: partial_trace(keep, optimize='auto-hq', simplify_sequence='ADCRS', simplify_atol=1e-12, simplify_equalize_norms=True, backend=None, dtype=None, rehearse=False) Perform the partial trace on the circuit wavefunction, retaining only qubits in ``keep``, and making use of reverse lightcone cancellation: .. math:: \rho_{\bar{q}} = Tr_{\bar{p}} |\psi_{\bar{q}} \rangle \langle \psi_{\bar{q}}| Where :math:`\bar{q}` is the set of qubits to keep, :math:`\psi_{\bar{q}}` is the circuit wavefunction only with gates in the causal cone of this set, and :math:`\bar{p}` is the remaining qubits. :param keep: The qubit(s) to keep as we trace out the rest. :type keep: int or sequence of int :param optimize: Contraction path optimizer to use for the reduced density matrix, can be a non-reusable path optimizer as only called once (though path won't be cached for later use in that case). :type optimize: str, optional :param simplify_sequence: Which local tensor network simplifications to perform and in which order, see :meth:`~quimb.tensor.tensor_core.TensorNetwork.full_simplify`. :type simplify_sequence: str, optional :param simplify_atol: The tolerance with which to compare to zero when applying :meth:`~quimb.tensor.tensor_core.TensorNetwork.full_simplify`. :type simplify_atol: float, optional :param simplify_equalize_norms: Actively renormalize tensor norms during simplification. :type simplify_equalize_norms: bool, optional :param backend: Backend to perform the marginal contraction with, e.g. ``'numpy'``, ``'cupy'`` or ``'jax'``. Passed to ``cotengra``. :type backend: str, optional :param dtype: Data type to cast the TN to before contraction. :type dtype: str, optional :param rehearse: If ``True``, generate and cache the simplified tensor network and contraction tree but don't actually perform the contraction. Returns a dict with keys ``"tn"`` and ``'tree'`` with the tensor network that will be contracted and the corresponding contraction tree if so. :type rehearse: bool or "tn", optional :rtype: array or dict .. py:attribute:: partial_trace_rehearse .. py:attribute:: partial_trace_tn .. py:method:: local_expectation(G, where, optimize='auto-hq', simplify_sequence='ADCRS', simplify_atol=1e-12, simplify_equalize_norms=True, backend=None, dtype=None, rehearse=False) Compute the a single expectation value of operator ``G``, acting on sites ``where``, making use of reverse lightcone cancellation. .. math:: \langle \psi_{\bar{q}} | G_{\bar{q}} | \psi_{\bar{q}} \rangle where :math:`\bar{q}` is the set of qubits :math:`G` acts one and :math:`\psi_{\bar{q}}` is the circuit wavefunction only with gates in the causal cone of this set. If you supply a tuple or list of gates then the expectations will be computed simultaneously. :param G: The raw operator(s) to find the expectation of. :type G: array or sequence[array] :param where: Which qubits the operator acts on. :type where: int or sequence of int :param optimize: Contraction path optimizer to use for the local expectation, can be a non-reusable path optimizer as only called once (though path won't be cached for later use in that case). :type optimize: str, optional :param simplify_sequence: Which local tensor network simplifications to perform and in which order, see :meth:`~quimb.tensor.tensor_core.TensorNetwork.full_simplify`. :type simplify_sequence: str, optional :param simplify_atol: The tolerance with which to compare to zero when applying :meth:`~quimb.tensor.tensor_core.TensorNetwork.full_simplify`. :type simplify_atol: float, optional :param simplify_equalize_norms: Actively renormalize tensor norms during simplification. :type simplify_equalize_norms: bool, optional :param backend: Backend to perform the marginal contraction with, e.g. ``'numpy'``, ``'cupy'`` or ``'jax'``. Passed to ``cotengra``. :type backend: str, optional :param dtype: Data type to cast the TN to before contraction. :type dtype: str, optional :param gate_opts: Options to use when applying ``G`` to the wavefunction. :type gate_opts: None or dict_like :param rehearse: If ``True``, generate and cache the simplified tensor network and contraction tree but don't actually perform the contraction. Returns a dict with keys ``'tn'`` and ``'tree'`` with the tensor network that will be contracted and the corresponding contraction tree if so. :type rehearse: bool or "tn", optional :rtype: scalar, tuple[scalar] or dict .. py:attribute:: local_expectation_rehearse .. py:attribute:: local_expectation_tn .. py:method:: compute_marginal(where, fix=None, optimize='auto-hq', backend=None, dtype='complex64', simplify_sequence='ADCRS', simplify_atol=1e-06, simplify_equalize_norms=True, rehearse=False) Compute the probability tensor of qubits in ``where``, given possibly fixed qubits in ``fix`` and tracing everything else having removed redundant unitary gates. :param where: The qubits to compute the marginal probability distribution of. :type where: sequence of int :param fix: Measurement results on other qubits to fix. :type fix: None or dict[int, str], optional :param optimize: Contraction path optimizer to use for the marginal, can be a non-reusable path optimizer as only called once (though path won't be cached for later use in that case). :type optimize: str, optional :param backend: Backend to perform the marginal contraction with, e.g. ``'numpy'``, ``'cupy'`` or ``'jax'``. Passed to ``cotengra``. :type backend: str, optional :param dtype: Data type to cast the TN to before contraction. :type dtype: str, optional :param simplify_sequence: Which local tensor network simplifications to perform and in which order, see :meth:`~quimb.tensor.tensor_core.TensorNetwork.full_simplify`. :type simplify_sequence: str, optional :param simplify_atol: The tolerance with which to compare to zero when applying :meth:`~quimb.tensor.tensor_core.TensorNetwork.full_simplify`. :type simplify_atol: float, optional :param simplify_equalize_norms: Actively renormalize tensor norms during simplification. :type simplify_equalize_norms: bool, optional :param rehearse: Whether to perform the marginal contraction or just return the associated TN and contraction tree. :type rehearse: bool or "tn", optional .. py:attribute:: compute_marginal_rehearse .. py:attribute:: compute_marginal_tn .. py:method:: calc_qubit_ordering(qubits=None, method='greedy-lightcone') Get a order to measure ``qubits`` in, by greedily choosing whichever has the smallest reverse lightcone followed by whichever expands this lightcone *least*. :param qubits: The qubits to generate a lightcone ordering for, if ``None``, assume all qubits. :type qubits: None or sequence of int :returns: The order to 'measure' qubits in. :rtype: tuple[int] .. py:method:: _parse_qubits_order(qubits=None, order=None) Simply initializes the default of measuring all qubits, and the default order, or checks that ``order`` is a permutation of ``qubits``. .. py:method:: _group_order(order, group_size=1) Take the qubit ordering ``order`` and batch it in groups of size ``group_size``, sorting the qubits (for caching reasons) within each group. .. py:method:: get_qubit_distances(method='dijkstra', alpha=2) Get a nested dictionary of qubit distances. This is computed from a graph representing qubit interactions. The graph has an edge between qubits if they are acted on by the same gate, and the distance-weight of the edge is exponentially small in the number of gates between them. :param method: The method to use to compute the qubit distances. See :func:`networkx.all_pairs_dijkstra_path_length` and :func:`networkx.resistance_distance`. :type method: {'dijkstra', 'resistance'}, optional :param alpha: The distance weight between qubits is ``alpha**(num_gates - 1 )``. :type alpha: float, optional :returns: The distance between each pair of qubits, accessed like ``distances[q1][q2]``. If two qubits are not connected, the distance is missing. :rtype: dict[int, dict[int, float]] .. py:method:: reordered_gates_dfs_clustered() Get the gates reordered by a depth first search traversal of the multi-qubit gate graph that greedily selects successive gates which are 'close' in graph distance, and shifts single qubit gates to be adjacent to multi-qubit gates where possible. .. py:method:: sample(C, qubits=None, order=None, group_size=10, max_marginal_storage=2**20, seed=None, optimize='auto-hq', backend=None, dtype='complex64', simplify_sequence='ADCRS', simplify_atol=1e-06, simplify_equalize_norms=True) Sample the circuit given by ``gates``, ``C`` times, using lightcone cancelling and caching marginal distribution results. This is a generator. This proceeds as a chain of marginal computations. Assuming we have ``group_size=1``, and some ordering of the qubits, :math:`\{q_0, q_1, q_2, q_3, \ldots\}` we first compute: .. math:: p(q_0) = \mathrm{diag} \mathrm{Tr}_{1, 2, 3,\ldots} | \psi_{0} \rangle \langle \psi_{0} | I.e. simply the probability distribution on a single qubit, conditioned on nothing. The subscript on :math:`\psi` refers to the fact that we only need gates from the causal cone of qubit 0. From this we can sample an outcome, either 0 or 1, if we call this :math:`r_0` we can then move on to the next marginal: .. math:: p(q_1 | r_0) = \mathrm{diag} \mathrm{Tr}_{2, 3,\ldots} \langle r_0 | \psi_{0, 1} \rangle \langle \psi_{0, 1} | r_0 \rangle I.e. the probability distribution of the next qubit, given our prior result. We can sample from this to get :math:`r_1`. Then we compute: .. math:: p(q_2 | r_0 r_1) = \mathrm{diag} \mathrm{Tr}_{3,\ldots} \langle r_0 r_1 | \psi_{0, 1, 2} \rangle \langle \psi_{0, 1, 2} | r_0 r_1 \rangle Eventually we will reach the 'final marginal', which we can compute as .. math:: |\langle r_0 r_1 r_2 r_3 \ldots | \psi \rangle|^2 since there is nothing left to trace out. :param C: The number of times to sample. :type C: int :param qubits: Which qubits to measure, defaults (``None``) to all qubits. :type qubits: None or sequence of int, optional :param order: Which order to measure the qubits in, defaults (``None``) to an order based on greedily expanding the smallest reverse lightcone. If specified it should be a permutation of ``qubits``. :type order: None or sequence of int, optional :param group_size: How many qubits to group together into marginals, the larger this is the fewer marginals need to be computed, which can be faster at the cost of higher memory. The marginal themselves will each be of size ``2**group_size``. :type group_size: int, optional :param max_marginal_storage: The total cumulative number of marginal probabilites to cache, once this is exceeded caching will be turned off. :type max_marginal_storage: int, optional :param seed: A random seed, passed to ``numpy.random.seed`` if given. :type seed: None or int, optional :param optimize: Contraction path optimizer to use for the marginals, shouldn't be a non-reusable path optimizer as called on many different TNs. Passed to :func:`cotengra.array_contract_tree`. :type optimize: str, optional :param backend: Backend to perform the marginal contraction with, e.g. ``'numpy'``, ``'cupy'`` or ``'jax'``. Passed to ``cotengra``. :type backend: str, optional :param dtype: Data type to cast the TN to before contraction. :type dtype: str, optional :param simplify_sequence: Which local tensor network simplifications to perform and in which order, see :meth:`~quimb.tensor.tensor_core.TensorNetwork.full_simplify`. :type simplify_sequence: str, optional :param simplify_atol: The tolerance with which to compare to zero when applying :meth:`~quimb.tensor.tensor_core.TensorNetwork.full_simplify`. :type simplify_atol: float, optional :param simplify_equalize_norms: Actively renormalize tensor norms during simplification. :type simplify_equalize_norms: bool, optional :Yields: **bitstrings** (*sequence of str*) .. py:method:: sample_rehearse(qubits=None, order=None, group_size=10, result=None, optimize='auto-hq', simplify_sequence='ADCRS', simplify_atol=1e-06, simplify_equalize_norms=True, rehearse=True, progbar=False) Perform the preparations and contraction tree findings for :meth:`~quimb.tensor.circuit.Circuit.sample`, caching various intermedidate objects, but don't perform the main contractions. :param qubits: Which qubits to measure, defaults (``None``) to all qubits. :type qubits: None or sequence of int, optional :param order: Which order to measure the qubits in, defaults (``None``) to an order based on greedily expanding the smallest reverse lightcone. :type order: None or sequence of int, optional :param group_size: How many qubits to group together into marginals, the larger this is the fewer marginals need to be computed, which can be faster at the cost of higher memory. The marginal's size itself is exponential in ``group_size``. :type group_size: int, optional :param result: Explicitly check the computational cost of this result, assumed to be all zeros if not given. :type result: None or dict[int, str], optional :param optimize: Contraction path optimizer to use for the marginals, shouldn't be a non-reusable path optimizer as called on many different TNs. Passed to :func:`cotengra.array_contract_tree`. :type optimize: str, optional :param simplify_sequence: Which local tensor network simplifications to perform and in which order, see :meth:`~quimb.tensor.tensor_core.TensorNetwork.full_simplify`. :type simplify_sequence: str, optional :param simplify_atol: The tolerance with which to compare to zero when applying :meth:`~quimb.tensor.tensor_core.TensorNetwork.full_simplify`. :type simplify_atol: float, optional :param simplify_equalize_norms: Actively renormalize tensor norms during simplification. :type simplify_equalize_norms: bool, optional :param progbar: Whether to show the progress of finding each contraction tree. :type progbar: bool, optional :returns: One contraction tree object per grouped marginal computation. The keys of the dict are the qubits the marginal is computed for, the values are a dict containing a representative simplified tensor network (key: 'tn') and the main contraction tree (key: 'tree'). :rtype: dict[tuple[int], dict] .. py:attribute:: sample_tns .. py:method:: sample_chaotic(C, marginal_qubits, fix=None, max_marginal_storage=2**20, seed=None, optimize='auto-hq', backend=None, dtype='complex64', simplify_sequence='ADCRS', simplify_atol=1e-06, simplify_equalize_norms=True) Sample from this circuit, *assuming* it to be chaotic. Which is to say, only compute and sample correctly from the final marginal, assuming that the distribution on the other qubits is uniform. Given ``marginal_qubits=5`` for instance, for each sample a random bit-string :math:`r_0 r_1 r_2 \ldots r_{N - 6}` for the remaining :math:`N - 5` qubits will be chosen, then the final marginal will be computed as .. math:: p(q_{N-5}q_{N-4}q_{N-3}q_{N-2}q_{N-1} | r_0 r_1 r_2 \ldots r_{N-6}) = |\langle r_0 r_1 r_2 \ldots r_{N - 6} | \psi \rangle|^2 and then sampled from. Note the expression on the right hand side has 5 open indices here and so is a tensor, however if ``marginal_qubits`` is not too big then the cost of contracting this is very similar to a single amplitude. .. note:: This method *assumes* the circuit is chaotic, if its not, then the samples produced will not be an accurate representation of the probability distribution. :param C: The number of times to sample. :type C: int :param marginal_qubits: The number of qubits to treat as marginal, or the actual qubits. If an int is given then the qubits treated as marginal will be ``circuit.calc_qubit_ordering()[:marginal_qubits]``. :type marginal_qubits: int or sequence of int :param fix: Measurement results on other qubits to fix. These will be randomly sampled if ``fix`` is not given or a qubit is missing. :type fix: None or dict[int, str], optional :param seed: A random seed, passed to ``numpy.random.seed`` if given. :type seed: None or int, optional :param optimize: Contraction path optimizer to use for the marginal, can be a non-reusable path optimizer as only called once (though path won't be cached for later use in that case). :type optimize: str, optional :param backend: Backend to perform the marginal contraction with, e.g. ``'numpy'``, ``'cupy'`` or ``'jax'``. Passed to ``cotengra``. :type backend: str, optional :param dtype: Data type to cast the TN to before contraction. :type dtype: str, optional :param simplify_sequence: Which local tensor network simplifications to perform and in which order, see :meth:`~quimb.tensor.tensor_core.TensorNetwork.full_simplify`. :type simplify_sequence: str, optional :param simplify_atol: The tolerance with which to compare to zero when applying :meth:`~quimb.tensor.tensor_core.TensorNetwork.full_simplify`. :type simplify_atol: float, optional :param simplify_equalize_norms: Actively renormalize tensor norms during simplification. :type simplify_equalize_norms: bool, optional :Yields: *str* .. py:method:: sample_chaotic_rehearse(marginal_qubits, result=None, optimize='auto-hq', simplify_sequence='ADCRS', simplify_atol=1e-06, simplify_equalize_norms=True, dtype='complex64', rehearse=True) Rehearse chaotic sampling (perform just the TN simplifications and contraction tree finding). :param marginal_qubits: The number of qubits to treat as marginal, or the actual qubits. If an int is given then the qubits treated as marginal will be ``circuit.calc_qubit_ordering()[:marginal_qubits]``. :type marginal_qubits: int or sequence of int :param result: Explicitly check the computational cost of this result, assumed to be all zeros if not given. :type result: None or dict[int, str], optional :param optimize: Contraction path optimizer to use for the marginal, can be a non-reusable path optimizer as only called once (though path won't be cached for later use in that case). :type optimize: str, optional :param simplify_sequence: Which local tensor network simplifications to perform and in which order, see :meth:`~quimb.tensor.tensor_core.TensorNetwork.full_simplify`. :type simplify_sequence: str, optional :param simplify_atol: The tolerance with which to compare to zero when applying :meth:`~quimb.tensor.tensor_core.TensorNetwork.full_simplify`. :type simplify_atol: float, optional :param simplify_equalize_norms: Actively renormalize tensor norms during simplification. :type simplify_equalize_norms: bool, optional :param dtype: Data type to cast the TN to before contraction. :type dtype: str, optional :returns: The contraction path information for the main computation, the key is the qubits that formed the final marginal. The value is itself a dict with keys ``'tn'`` - a representative tensor network - and ``'tree'`` - the contraction tree. :rtype: dict[tuple[int], dict] .. py:attribute:: sample_chaotic_tn .. py:method:: get_gate_by_gate_circuits(group_size=10) Get a sequence of circuits by partitioning the gates into groups such circuit `i + 1` acts on at most ``group_size`` new qubits compared to circuit `i`. :param group_size: The maximum number of new qubits that can be acted on by a circuit compared to its predecessor. :type group_size: int, optional :returns: A sequence of dicts, each with keys ``'circuit'`` and ``'where'``, where the former is a :class:`~quimb.tensor.circuit.Circuit` and the latter the tuple of new qubits that it acts on comparaed to the previous circuit. :rtype: Sequence[dict] .. py:method:: sample_gate_by_gate(C, group_size=10, seed=None, max_marginal_storage=2**20, optimize='auto-hq', backend=None, dtype='complex64', simplify_sequence='ADCRS', simplify_atol=1e-06, simplify_equalize_norms=True) Sample this circuit using the gate-by-gate method, where we 'evolve' a result bitstring by sequentially including more and more gates, at each step updating the result by computing a full conditional marginal. See "How to simulate quantum measurement without computing marginals" by Sergey Bravyi, David Gosset, Yinchen Liu (https://arxiv.org/abs/2112.08499). The overall complexity of this is guaranteed to be similar to that of computing a single amplitude which can be much better than the naive "qubit-by-qubit" (`.sample`) method. However, it requires evaluting a number of tensor networks that scales linearly with the number of gates which can offset any practical advantages for shallow circuits for example. :param C: The number of samples to generate. :type C: int :param group_size: The maximum number of qubits that can be acted on by a circuit compared to its predecessor. This will be the dimension of the marginal computed at each step. :type group_size: int, optional :param seed: A random seed, passed to ``numpy.random.seed`` if given. :type seed: None or int, optional :param max_marginal_storage: The total cumulative number of marginal probabilites to cache, once this is exceeded caching will be turned off. :type max_marginal_storage: int, optional :param optimize: Contraction path optimizer to use for the marginals, shouldn't be a non-reusable path optimizer as called on many different TNs. Passed to :func:`cotengra.array_contract_tree`. :type optimize: str, optional :param backend: Backend to perform the marginal contraction with, e.g. ``'numpy'``, ``'cupy'`` or ``'jax'``. Passed to ``cotengra``. :type backend: str, optional :param dtype: Data type to cast the TN to before contraction. :type dtype: str, optional :param simplify_sequence: Which local tensor network simplifications to perform and in which order, see :meth:`~quimb.tensor.tensor_core.TensorNetwork.full_simplify`. :type simplify_sequence: str, optional :param simplify_atol: The tolerance with which to compare to zero when applying :meth:`~quimb.tensor.tensor_core.TensorNetwork.full_simplify`. :type simplify_atol: float, optional :param simplify_equalize_norms: Actively renormalize tensor norms during simplification. :type simplify_equalize_norms: bool, optional :param rehearse: If ``True``, generate and cache the simplified tensor network and contraction tree but don't actually perform the contraction. Returns a dict with keys ``'tn'`` and ``'tree'`` with the tensor network that will be contracted and the corresponding contraction tree if so. :type rehearse: bool, optional :Yields: *str* .. py:method:: sample_gate_by_gate_rehearse(group_size=10, optimize='auto-hq', dtype='complex64', simplify_sequence='ADCRS', simplify_atol=1e-06, simplify_equalize_norms=True, rehearse=True, progbar=False) Perform the preparations and contraction tree findings for :meth:`~quimb.tensor.circuit.Circuit.sample_gate_by_gate`, caching various intermedidate objects, but don't perform the main contractions. :param group_size: The maximum number of qubits that can be acted on by a circuit compared to its predecessor. This will be the dimension of the marginal computed at each step. :type group_size: int, optional :param optimize: Contraction path optimizer to use for the marginals, shouldn't be a non-reusable path optimizer as called on many different TNs. Passed to :func:`cotengra.array_contract_tree`. :type optimize: str, optional :param dtype: Data type to cast the TN to before contraction. :type dtype: str, optional :param simplify_sequence: Which local tensor network simplifications to perform and in which order, see :meth:`~quimb.tensor.tensor_core.TensorNetwork.full_simplify`. :type simplify_sequence: str, optional :param simplify_atol: The tolerance with which to compare to zero when applying :meth:`~quimb.tensor.tensor_core.TensorNetwork.full_simplify`. :type simplify_atol: float, optional :param simplify_equalize_norms: Actively renormalize tensor norms during simplification. :type simplify_equalize_norms: bool, optional :param rehearse: If ``True``, generate and cache the simplified tensor network and contraction tree but don't actually perform the contraction. If "tn", only generate the simplified tensor networks. :type rehearse: True or "tn", optional :rtype: Sequence[dict] or Sequence[TensorNetwork] .. py:attribute:: sample_gate_by_gate_tns .. py:method:: to_dense(reverse=False, optimize='auto-hq', simplify_sequence='R', simplify_atol=1e-12, simplify_equalize_norms=True, backend=None, dtype=None, rehearse=False) Generate the dense representation of the final wavefunction. :param reverse: Whether to reverse the order of the subsystems, to match the convention of qiskit for example. :type reverse: bool, optional :param optimize: Contraction path optimizer to use for the contraction, can be a non-reusable path optimizer as only called once (though path won't be cached for later use in that case). :type optimize: str, optional :param dtype: If given, convert the tensors to this dtype prior to contraction. :type dtype: dtype or str, optional :param simplify_sequence: Which local tensor network simplifications to perform and in which order, see :meth:`~quimb.tensor.tensor_core.TensorNetwork.full_simplify`. :type simplify_sequence: str, optional :param simplify_atol: The tolerance with which to compare to zero when applying :meth:`~quimb.tensor.tensor_core.TensorNetwork.full_simplify`. :type simplify_atol: float, optional :param simplify_equalize_norms: Actively renormalize tensor norms during simplification. :type simplify_equalize_norms: bool, optional :param backend: Backend to perform the contraction with, e.g. ``'numpy'``, ``'cupy'`` or ``'jax'``. Passed to ``cotengra``. :type backend: str, optional :param dtype: Data type to cast the TN to before contraction. :type dtype: str, optional :param rehearse: If ``True``, generate and cache the simplified tensor network and contraction tree but don't actually perform the contraction. Returns a dict with keys ``'tn'`` and ``'tree'`` with the tensor network that will be contracted and the corresponding contraction tree if so. :type rehearse: bool, optional :returns: **psi** -- The densely represented wavefunction with ``dtype`` data. :rtype: qarray .. py:attribute:: to_dense_rehearse .. py:attribute:: to_dense_tn .. py:method:: schrodinger_contract(*args, **contract_opts) .. py:method:: xeb_ex(optimize='auto-hq', simplify_sequence='R', simplify_atol=1e-12, simplify_equalize_norms=True, dtype=None, backend=None, autojit=False, progbar=False, **contract_opts) Compute the exactly expected XEB for this circuit. The main feature here is that if you supply a cotengra optimizer that searches for sliced indices then the XEB will be computed without constructing the full wavefunction. :param optimize: Contraction path optimizer. :type optimize: str or PathOptimizer, optional :param simplify_sequence: Simplifications to apply to tensor network prior to contraction. :type simplify_sequence: str, optional :param simplify_sequence: Which local tensor network simplifications to perform and in which order, see :meth:`~quimb.tensor.tensor_core.TensorNetwork.full_simplify`. :type simplify_sequence: str, optional :param simplify_atol: The tolerance with which to compare to zero when applying :meth:`~quimb.tensor.tensor_core.TensorNetwork.full_simplify`. :type simplify_atol: float, optional :param dtype: Data type to cast the TN to before contraction. :type dtype: str, optional :param backend: Convert tensors to, and then use contractions from, this library. :type backend: str, optional :param autojit: Apply ``autoray.autojit`` to the contraciton and map-reduce. :type autojit: bool, optional :param progbar: Show progress in terms of number of wavefunction chunks processed. :type progbar: bool, optional .. py:method:: apply_gates(gates, progbar=False, **gate_opts) Apply a sequence of gates to this tensor network quantum circuit. :param gates: The sequence of gates to apply. :type gates: Sequence[Gate] or Sequence[Tuple] :param gate_opts: Supplied to :meth:`~quimb.tensor.circuit.Circuit.apply_gate`. .. py:class:: CircuitDense(N=None, psi0=None, gate_opts=None, gate_contract=True, tags=None, convert_eager=True, **circuit_opts) Bases: :py:obj:`Circuit` Quantum circuit simulation keeping the state in full dense form. .. py:method:: get_psi() Get the dense wavefunction as a length one tensor network, with a ``Dense1D`` view. .. py:method:: get_uni(transposed=False) :abstractmethod: Tensor network representation of the unitary operator (i.e. with the initial state removed). .. py:method:: calc_qubit_ordering(qubits=None) Qubit ordering doesn't matter for a dense wavefunction. .. py:method:: get_psi_reverse_lightcone(where, keep_psi0=False) Override ``get_psi_reverse_lightcone`` as for a dense wavefunction the lightcone is not meaningful.