quimb.tensor.circuit.core¶
Representation-agnostic quantum circuit interface (CircuitBase).
Classes¶
Representation-agnostic interface shared by every circuit simulator. |
Module Contents¶
- class quimb.tensor.circuit.core.CircuitBase(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)[source]¶
Representation-agnostic interface shared by every circuit simulator.
Holds the circuit description (the list of
Gateobjects, gate application front-end and convenience methods, named-parameter management, backend/dtype conversion, thefrom_*constructors, drawing) plus the generic_apply_gatedispatch. It carries no exact-contraction machinery, so representation-specific simulators can compose this interface without inheriting the exactCircuit.Subclasses must implement the
_init_stateandpsihooks and may overridecalc_qubit_ordering(the default is a trivial sorted order).Notes
The named-parameter methods (
register_named_params/get_params/set_params/update_params_from) index gate tensors by tag, so they are only functional whentag_gate_numbers=True(the exactCircuitdefault). Representations that disable gate-number tagging (MPS/PEPS/PEPO) inherit them but they are non-functional there.- tag_gate_numbers = True¶
- tag_gate_rounds = True¶
- tag_gate_labels = True¶
- dtype = None¶
- to_backend = None¶
- convert_eager = False¶
- _backend_gate_cache¶
- gate_opts¶
- _gates = []¶
- _ket_site_ind_id¶
- _bra_site_ind_id = 'b{}'¶
- _gate_tag_id = 'GATE_{}'¶
- _round_tag_id = 'ROUND_{}'¶
- _sample_n_gates = -1¶
- _storage¶
- _sampled_conditionals¶
- _named_params¶
- _named_param_exprs¶
- property named_params¶
Named circuit parameters and their current values.
- property named_param_names¶
Names of registered circuit parameters.
- property param_expressions¶
Gate parameter expressions keyed by gate index.
- register_named_params(named_params, gate_expressions=None)[source]¶
Register named circuit parameters and gate dependencies.
- Parameters:
named_params (sequence[str] or mapping[str, scalar]) – Either names to register, which default to
nanuntil bound, or a mapping supplying initial values.gate_expressions (mapping[int, tuple], optional) – Mapping from gate index to the expressions used to generate that gate’s parameters. Each expression can be a constant, a string expression referencing the named parameters, or a callable taking the current named parameter mapping.
- get_params()[source]¶
Get a pytree - in this case a dict - of all the parameters in the circuit.
- Returns:
Dictionary containing any named parameters plus any directly parametrized gates not driven by named parameter expressions.
- Return type:
- set_params(params)[source]¶
Set the parameters of the circuit.
- Parameters:
params (dict) – Dictionary mapping gate numbers and/or registered named parameter names to new values.
- classmethod from_qsim_str(contents, progbar=False, **circuit_opts)[source]¶
Generate a
Circuitinstance from a ‘qsim’ string.
- classmethod from_qsim_file(fname, progbar=False, **circuit_opts)[source]¶
Generate a
Circuitinstance from a ‘qsim’ file.The qsim file format is described here: https://quantumai.google/qsim/input_format.
- classmethod from_qsim_url(url, progbar=False, **circuit_opts)[source]¶
Generate a
Circuitinstance from a ‘qsim’ url.
- classmethod from_openqasm2_str(contents, progbar=False, **circuit_opts)[source]¶
Generate a
Circuitinstance from an OpenQASM 2.0 string.
- classmethod from_openqasm2_file(fname, progbar=False, **circuit_opts)[source]¶
Generate a
Circuitinstance from an OpenQASM 2.0 file.
- classmethod from_openqasm2_url(url, progbar=False, **circuit_opts)[source]¶
Generate a
Circuitinstance from an OpenQASM 2.0 url.
- classmethod from_openqasm3_str(contents, progbar=False, **circuit_opts)[source]¶
Construct a circuit from an OpenQASM 3.0 string.
- Parameters:
- Returns:
A circuit populated with the parsed gates. If symbolic
inputdeclarations are present, they are registered as generic named circuit parameters so thatset_params()can bind them later.- Return type:
- classmethod from_openqasm3_file(fname, progbar=False, **circuit_opts)[source]¶
Construct a circuit from an OpenQASM 3.0 file.
- classmethod from_openqasm3_url(url, progbar=False, **circuit_opts)[source]¶
Construct a circuit from an OpenQASM 3.0 URL.
- classmethod from_gates(gates, N=None, progbar=False, **kwargs)[source]¶
Generate a
Circuitinstance from a sequence of gates.
- property gates¶
- property num_gates¶
- _apply_gate(gate, tags=None, **gate_opts)[source]¶
Apply a
Gateto thisCircuit. This is the main method that all calls to apply a gate should go through.
- apply_gate(gate_id, *gate_args, params=None, qubits=None, controls=None, gate_round=None, parametrize=None, **gate_opts)[source]¶
Apply a single gate to this tensor network quantum circuit. If
gate_roundis supplied the tensor(s) added will be tagged with'ROUND_{gate_round}'. Alternatively, putting an integer first like so:circuit.apply_gate(10, 'H', 7)
Is automatically translated to:
circuit.apply_gate('H', 7, gate_round=10)
- Parameters:
gate_id (Gate, str, or array_like) –
Which gate to apply. This can be:
A
Gateinstance, i.e. with parameters and qubits already specified.A string, e.g.
'H','U3', etc. in which casegate_argsshould be supplied with(*params, *qubits).A raw array, in which case
gate_argsshould be supplied with(*qubits,).
gate_round (int, optional) – The gate round. If
gate_idis integer-like, will also be taken from here, with thengate_id, gate_args = gate_args[0], gate_args[1:].gate_opts – Supplied to the gate function, options here will override the default
gate_opts.
- apply_gate_raw(U, where, controls=None, gate_round=None, **gate_opts)[source]¶
Apply the raw array
Uas a gate on qubits inwhere. It will be assumed to be unitary for the sake of computing reverse lightcones.
- apply_gates(gates, progbar=False, **gate_opts)[source]¶
Apply a sequence of gates to this tensor network quantum circuit.
- Parameters:
gates (Sequence[Gate] or Sequence[Tuple]) – The sequence of gates to apply.
gate_opts – Supplied to
apply_gate().
- su4(theta1, phi1, lamda1, theta2, phi2, lamda2, theta3, phi3, lamda3, theta4, phi4, lamda4, t1, t2, t3, i, j, gate_round=None, parametrize=False, **kwargs)[source]¶
- simulate_counts(C, seed=None, reverse=False, **to_dense_opts)[source]¶
Simulate measuring all qubits in the computational basis many times. Unlike
sample(), this generates all the samples simultaneously using the full wavefunction constructed fromto_dense(), then callingsimulate_counts().Warning
Because this constructs the full wavefunction it always requires exponential memory in the number of qubits, regardless of circuit depth and structure.
- Parameters:
- Returns:
results – The number of recorded counts for each
- Return type:
- xeb(samples_or_counts, cache=None, cache_maxsize=2**20, progbar=False, **amplitude_opts)[source]¶
Compute the linear cross entropy benchmark (XEB) for samples or counts, amplitude per amplitude.
- Parameters:
samples_or_counts (Iterable[str] or Dict[str, int]) – Either the raw bitstring samples or a dict mapping bitstrings to the number of counts observed.
cache (dict, optional) – A dictionary to store the probabilities in, if not supplied
quimb.utils.LRU(cache_maxsize)will be used.cache_maxsize – The maximum size of the cache to be used.
optional – The maximum size of the cache to be used.
progbar – Whether to show progress as the bitstrings are iterated over.
optional – Whether to show progress as the bitstrings are iterated over.
amplitude_opts – Supplied to
amplitude().
- update_params_from(tn)[source]¶
Assuming
tnis a tensor network with tensors taggedGATE_{i}corresponding to this circuit (e.g. fromcirc.psiorcirc.uni) but with updated parameters, update the current circuit parameters and tensors with those values.This is an inplace modification of the
Circuit.- Parameters:
tn (TensorNetwork) – The tensor network to find the updated parameters from.
- draw(figsize=None, radius=1 / 3, drawcolor=(0.5, 0.5, 0.5), linewidth=1)[source]¶
Draw a simple linear schematic of the circuit.
- Parameters:
- Returns:
fig (matplotlib.Figure) – The figure object.
ax (matplotlib.Axes) – The axis object.