tangelo.linq.target package

Submodules

tangelo.linq.target.backend module

Base class to define backends (simulators or actual devices) and abstracting their differences from the user. Users can define their own backend as a child class of this one. Able to run noiseless and noisy simulations, leveraging the capabilities of different backends, quantum or classical.

If the user provides a noise model, then a noisy simulation is run with n_shots shots. If the user only provides n_shots, a noiseless simulation is run, drawing the desired amount of shots. If the target backend has access to the statevector representing the quantum state, we leverage any kind of emulation available to reduce runtime (directly generating shot values from final statevector etc). If the quantum circuit contains a MEASURE instruction, it is assumed to simulate a mixed-state and the simulation will be carried by simulating individual shots (e.g a number of shots is required).

Some backends may only support a subset of the above. This information is contained in the static method backend_info defined by each subclass target.

class tangelo.linq.target.backend.Backend(n_shots=None, noise_model=None)

Bases: ABC

abstract static backend_info() dict

A dictionary that includes {‘noisy_simulation’: True or False, ‘statevector_available’: True or False, ‘statevector_order’: ‘lsq_first’ or ‘msq_first’

collapse_statevector_to_desired_measurement(statevector, qubit, result, ignore_zero_prob=False)

Take 0 or 1 part of a statevector for a given qubit and return a normalized statevector and probability.

Parameters:
  • statevector (array) – The statevector for which the collapse to the desired qubit value is performed.

  • qubit (int) – The index of the qubit to collapse to the classical result.

  • result (string) – “0” or “1”.

  • ignore_zero_prob (bool) – If True, a vector of zeros is returned if the probability is zero. Default value set to False.

Returns:
  • array – the collapsed and renormalized statevector

  • float – the probability this occurred.

get_expectation_value(qubit_operator, state_prep_circuit, initial_statevector=None, desired_meas_result=None)

Take as input a qubit operator H and a quantum circuit preparing a state |psi>. Return the expectation value <psi | H | psi>.

In the case of a noiseless simulation, if the target backend exposes the statevector then it is used directly to compute expectation values, or draw samples if required. In the case of a noisy simulator, or if the statevector is not available on the target backend, individual shots must be run and the workflow is akin to what we would expect from an actual QPU.

Parameters:
  • qubit_operator (QubitOperator) – the qubit operator.

  • state_prep_circuit (Circuit) – an abstract circuit used for state preparation.

  • initial_statevector (array) – The initial statevector for the simulation

  • desired_meas_result (str) – The mid-circuit measurement results to select for.

Returns:

complex – The expectation value of this operator with regards to the state preparation.

static get_expectation_value_from_frequencies_oneterm(term, frequencies)

Return the expectation value of a single-term qubit-operator, given the result of a state-preparation.

Parameters:
  • term (QubitOperator) – a single-term qubit operator

  • frequencies (dict) – histogram of frequencies of measurements (assumed to be in lsq-first format).

Returns:

complex – The expectation value of this operator with regard to the state preparation.

get_standard_error(qubit_operator, state_prep_circuit, initial_statevector=None, desired_meas_result=None)

Take as input a qubit operator H and a quantum circuit preparing a state |psi>. Return the standard error of <psi | H | psi>, e.g. sqrt(Var H / n_shots).

In the case of a noiseless simulation, if the target backend exposes the statevector then it is used directly to compute standard error (zero), or draw samples if required. In the case of a noisy simulator, or if the statevector is not available on the target backend, individual shots must be run and the workflow is akin to what we would expect from an actual QPU.

Parameters:
  • qubit_operator (QubitOperator) – the qubit operator.

  • state_prep_circuit (Circuit) – an abstract circuit used for state preparation.

  • initial_statevector (list/array) – A valid statevector in the format supported by the target backend.

  • desired_meas_result (str) – The mid-circuit measurement results to select for.

Returns:

complex – The standard error of this operator with regard to the state preparation.

get_variance(qubit_operator, state_prep_circuit, initial_statevector=None, desired_meas_result=None)

Take as input a qubit operator H and a quantum circuit preparing a state |psi>. Return the variance <psi | H | psi>.

In the case of a noiseless simulation, if the target backend exposes the statevector then it is used directly to compute variance (zero), or draw samples if required. In the case of a noisy simulator, or if the statevector is not available on the target backend, individual shots must be run and the workflow is akin to what we would expect from an actual QPU.

Parameters:
  • qubit_operator (QubitOperator) – the qubit operator.

  • state_prep_circuit (Circuit) – an abstract circuit used for state preparation.

  • initial_statevector (list/array) – A valid statevector in the format supported by the target backend.

  • desired_meas_result (str) – The mid-circuit measurement results to select for.

Returns:

complex – The variance of this operator with regard to the state preparation.

static get_variance_from_frequencies_oneterm(term, frequencies)

Return the variance of a single-term qubit-operator, given the result of a state-preparation.

Parameters:
  • term (QubitOperator) – a single-term qubit operator.

  • frequencies (dict) – histogram of frequencies of measurements (assumed to be in lsq-first format).

Returns:

complex – The variance of this operator with regard to the state preparation.

perform_measurement(statevector, qubit, desired_meas_result=None)

Perform a measurement and return the new statevector and the probability that measurement occurred

Parameters:
  • statevector (array) – The statevector to perform the measurement on.

  • qubit (int) – The qubit to apply the measurement to

  • desired_meas_result (Union[int, None]) – The desired measurement result, Default None

Returns:
  • str – The result of the measurement

  • array – The measured and renormalized statevector

  • float – The probability this measurement occurred

simulate(source_circuit, return_statevector=False, initial_statevector=None, desired_meas_result=None, save_mid_circuit_meas=False)

Perform state preparation corresponding to the input circuit on the target backend, return the frequencies of the different observables, and either the statevector or None depending on the availability of the statevector and if return_statevector is set to True. For the statevector backends supporting it, an initial statevector can be provided to initialize the quantum state without simulating all the equivalent gates.

Parameters:
  • source_circuit (Circuit) – a circuit in the abstract format to be translated for the target backend.

  • return_statevector (bool) – option to return the statevector as well, if available.

  • initial_statevector (list/array) – A valid statevector in the format supported by the target backend.

  • desired_meas_result (str) – The binary string of the desired measurement. Must have the same length as the number of MEASURE gates in source_circuit If self.n_shots is set, statistics are performed assuming self.n_shots successes

  • save_mid_circuit_meas (bool) – Save mid-circuit measurement results to self.mid_circuit_meas_freqs. All measurements will be saved to self.all_frequencies, with keys of length (n_meas + n_qubits). The leading n_meas values will hold the results of the MEASURE gates, ordered by their appearance in the source_circuit. The last n_qubits values will hold the measurements performed on each of qubits at the end of the circuit.

Returns:
  • dict – A dictionary mapping multi-qubit states to their corresponding frequency.

  • numpy.array – The statevector, if available for the target backend and requested by the user (if not, set to None).

abstract simulate_circuit()

Perform state preparation corresponding to the input circuit on the target backend, return the frequencies of the different observables, and either the statevector or None depending on the availability of the statevector and if return_statevector is set to True. For the statevector backends supporting it, an initial statevector can be provided to initialize the quantum state without simulating all the equivalent gates.

Parameters:
  • source_circuit (Circuit) – a circuit in the abstract format to be translated for the target backend.

  • return_statevector (bool) – option to return the statevector as well, if available.

  • initial_statevector (list/array) – A valid statevector in the format supported by the target backend.

  • desired_meas_result (str) – The binary string of the desired measurement. Must have the same length as the number of MEASURE gates in source_circuit

  • save_mid_circuit_meas (bool) – Save mid-circuit measurement results to self.mid_circuit_meas_freqs. All measurements will be saved to self.all_frequencies, with keys of length (n_meas + n_qubits). The leading n_meas values will hold the results of the MEASURE gates, ordered by their appearance in the source_circuit. The last n_qubits values will hold the measurements performed on each of qubits at the end of the circuit.

Returns:
  • dict – A dictionary mapping multi-qubit states to their corresponding frequency.

  • numpy.array – The statevector, if available for the target backend and requested by the user (if not, set to None).

tangelo.linq.target.backend.collapse_statevector_to_desired_measurement(statevector, qubit, result, order='lsq_first', ignore_zero_prob=False)

Take 0 or 1 part of a statevector for a given qubit and return a normalized statevector and probability.

Parameters:
  • statevector (array) – The statevector for which the collapse to the desired qubit value is performed.

  • qubit (int) – Index of target qubit to collapse in the desired classical state.

  • result (int) – 0 or 1.

  • order (string) – The qubit ordering of the statevector, lsq_first or msq_first.

  • ignore_zero_prob (bool) – Default False, If True, return zero vector if probability is below 1.e-14

Returns:
  • array – The collapsed and renormalized statevector.

  • float – The probability for the desired measurement to occur.

tangelo.linq.target.backend.get_expectation_value_from_frequencies_oneterm(term, frequencies)

Return the expectation value of a single-term qubit-operator, given the result of a state-preparation.

Parameters:
  • term (QubitOperator) – a single-term qubit operator.

  • frequencies (dict) – histogram of frequencies of measurements (assumed to be in lsq-first format).

Returns:

complex – The expectation value of this operator with regard to the state preparation.

tangelo.linq.target.backend.get_variance_from_frequencies_oneterm(term, frequencies)

Return the variance of the expectation value of a single-term qubit-operator, given the result of a state-preparation. :param term: a single-term qubit operator. :type term: QubitOperator :param frequencies: histogram of frequencies of measurements (assumed

to be in lsq-first format).

Returns:

complex – The variance of this operator with regard to the state preparation.

tangelo.linq.target.target_cirq module

class tangelo.linq.target.target_cirq.CirqSimulator(n_shots=None, noise_model=None)

Bases: Backend

static backend_info()

A dictionary that includes {‘noisy_simulation’: True or False, ‘statevector_available’: True or False, ‘statevector_order’: ‘lsq_first’ or ‘msq_first’

expectation_value_from_prepared_state(qubit_operator, n_qubits, prepared_state)

Compute an expectation value using a representation of the state (density matrix, state vector…) using Cirq functionalities.

Parameters:
  • qubit_operator (QubitOperator) – a qubit operator in tangelo format

  • n_qubits (int) – the number of qubits the operator acts on

  • prepared_state (np.array) – a numpy array encoding the state (can be a vector or a matrix)

Returns:

float64 – the expectation value of the qubit operator w.r.t the input state

simulate_circuit(source_circuit: Circuit, return_statevector=False, initial_statevector=None, desired_meas_result=None, save_mid_circuit_meas=False)

Perform state preparation corresponding to the input circuit on the target backend, return the frequencies of the different observables, and either the statevector or None depending on the availability of the statevector and if return_statevector is set to True. For the statevector backends supporting it, an initial statevector can be provided to initialize the quantum state without simulating all the equivalent gates.

Parameters:
  • source_circuit (Circuit) – a circuit in the abstract format to be translated for the target backend.

  • return_statevector (bool) – option to return the statevector as well, if available.

  • initial_statevector (list/array) – A valid statevector in the format supported by the target backend.

  • desired_meas_result (str) – The binary string of the desired measurement. Must have the same length as the number of MEASURE gates in source_circuit

  • save_mid_circuit_meas (bool) – Save mid-circuit measurement results to self.mid_circuit_meas_freqs. All measurements will be saved to self.all_frequencies, with keys of length (n_meas + n_qubits). The leading n_meas values will hold the results of the MEASURE gates, ordered by their appearance in the source_circuit. The last n_qubits values will hold the measurements performed on each of qubits at the end of the circuit.

Returns:
  • dict – A dictionary mapping multi-qubit states to their corresponding frequency.

  • numpy.array – The statevector, if available for the target backend and requested by the user (if not, set to None).

tangelo.linq.target.target_qdk module

class tangelo.linq.target.target_qdk.QDKSimulator(n_shots=None, noise_model=None)

Bases: Backend

static backend_info()

A dictionary that includes {‘noisy_simulation’: True or False, ‘statevector_available’: True or False, ‘statevector_order’: ‘lsq_first’ or ‘msq_first’

simulate_circuit(source_circuit: Circuit, return_statevector=False, initial_statevector=None, desired_meas_result=None, save_mid_circuit_meas=False)

Perform state preparation corresponding to the input circuit on the target backend, return the frequencies of the different observables, and either the statevector or None depending on the availability of the statevector and if return_statevector is set to True. For the statevector backends supporting it, an initial statevector can be provided to initialize the quantum state without simulating all the equivalent gates.

Parameters:
  • source_circuit (Circuit) – a circuit in the abstract format to be translated for the target backend.

  • return_statevector (bool) – option to return the statevector as well, if available.

  • initial_statevector (list/array) – A valid statevector in the format supported by the target backend.

  • desired_meas_result (str) – The binary string of the desired measurement. Must have the same length as the number of MEASURE gates in source_circuit

  • save_mid_circuit_meas (bool) – Save mid-circuit measurement results to self.mid_circuit_meas_freqs. All measurements will be saved to self.all_frequencies, with keys of length (n_meas + n_qubits). The leading n_meas values will hold the results of the MEASURE gates, ordered by their appearance in the source_circuit. The last n_qubits values will hold the measurements performed on each of qubits at the end of the circuit.

Returns:
  • dict – A dictionary mapping multi-qubit states to their corresponding frequency.

  • numpy.array – The statevector, if available for the target backend and requested by the user (if not, set to None).

tangelo.linq.target.target_qiskit module

class tangelo.linq.target.target_qiskit.QiskitSimulator(n_shots=None, noise_model=None)

Bases: Backend

Interface to the qiskit simulator.

static backend_info()

A dictionary that includes {‘noisy_simulation’: True or False, ‘statevector_available’: True or False, ‘statevector_order’: ‘lsq_first’ or ‘msq_first’

simulate_circuit(source_circuit: Circuit, return_statevector=False, initial_statevector=None, desired_meas_result=None, save_mid_circuit_meas=False)

Perform state preparation corresponding to the input circuit on the target backend, return the frequencies of the different observables, and either the statevector or None depending on the availability of the statevector and if return_statevector is set to True. For the statevector backends supporting it, an initial statevector can be provided to initialize the quantum state without simulating all the equivalent gates.

Parameters:
  • source_circuit (Circuit) – a circuit in the abstract format to be translated for the target backend.

  • return_statevector (bool) – option to return the statevector as well, if available.

  • initial_statevector (list/array) – A valid statevector in the format supported by the target backend.

  • desired_meas_result (str) – The binary string of the desired measurement. Must have the same length as the number of MEASURE gates in source_circuit

  • save_mid_circuit_meas (bool) – Save mid-circuit measurement results to self.mid_circuit_meas_freqs. All measurements will be saved to self.all_frequencies, with keys of length (n_meas + n_qubits). The leading n_meas values will hold the results of the MEASURE gates, ordered by their appearance in the source_circuit. The last n_qubits values will hold the measurements performed on each of qubits at the end of the circuit.

Returns:
  • dict – A dictionary mapping multi-qubit states to their corresponding frequency.

  • numpy.array – The statevector, if available for the target backend and requested by the user (if not, set to None).

tangelo.linq.target.target_qulacs module

class tangelo.linq.target.target_qulacs.QulacsSimulator(n_shots=None, noise_model=None)

Bases: Backend

static backend_info()

A dictionary that includes {‘noisy_simulation’: True or False, ‘statevector_available’: True or False, ‘statevector_order’: ‘lsq_first’ or ‘msq_first’

expectation_value_from_prepared_state(qubit_operator, n_qubits, prepared_state=None)

Compute an expectation value using a representation of the state using qulacs functionalities.

Parameters:
  • qubit_operator (QubitOperator) – a qubit operator in tangelo format

  • n_qubits (int) – Number of qubits.

  • prepared_state (np.array) – a numpy array encoding the state (can be a vector or a matrix). It is internally transformed into a qulacs.QuantumState object. Default is None, in this case it is set to the current state in the simulator object.

Returns:

float64 – the expectation value of the qubit operator w.r.t the input state

simulate_circuit(source_circuit: Circuit, return_statevector=False, initial_statevector=None, desired_meas_result=None, save_mid_circuit_meas=False)

Perform state preparation corresponding to the input circuit on the target backend, return the frequencies of the different observables, and either the statevector or None depending on the availability of the statevector and if return_statevector is set to True. For the statevector backends supporting it, an initial statevector can be provided to initialize the quantum state without simulating all the equivalent gates.

Parameters:
  • source_circuit (Circuit) – a circuit in the abstract format to be translated for the target backend.

  • return_statevector (bool) – option to return the statevector as well, if available.

  • initial_statevector (list/array) – A valid statevector in the format supported by the target backend.

  • desired_meas_result (str) – The binary string of the desired measurement. Must have the same length as the number of MEASURE gates in source_circuit

  • save_mid_circuit_meas (bool) – Save mid-circuit measurement results to self.mid_circuit_meas_freqs. All measurements will be saved to self.all_frequencies, with keys of length (n_meas + n_qubits). The leading n_meas values will hold the results of the MEASURE gates, ordered by their appearance in the source_circuit. The last n_qubits values will hold the measurements performed on each of qubits at the end of the circuit.

Returns:
  • dict – A dictionary mapping multi-qubit states to their corresponding frequency.

  • numpy.array – The statevector, if available for the target backend and requested by the user (if not, set to None).

tangelo.linq.target.target_stim module

The stim simulation package is made for fast simulation of Clifford circuits. See https://arxiv.org/abs/2103.02202v3 for more details.

class tangelo.linq.target.target_stim.StimSimulator(n_shots=None, noise_model=None)

Bases: Backend

Interface to the stim simulator

static backend_info()

A dictionary that includes {‘noisy_simulation’: True or False, ‘statevector_available’: True or False, ‘statevector_order’: ‘lsq_first’ or ‘msq_first’

simulate_circuit(source_circuit: Circuit, return_statevector=False, initial_statevector=None, desired_meas_result=None, save_mid_circuit_meas=False)

Perform state preparation corresponding to the input circuit on the target backend, return the frequencies of the different observables, and either the statevector or None depending on if return_statevector is set to True. The initial_statevector, and desired_meas_result features are currently not implemented and will return and error if not None.

Parameters:
  • source_circuit (Circuit) – a circuit in the abstract format to be translated for the target backend.

  • return_statevector (bool) – option to return the statevector

  • initial_statevector (list/array) – Not currently implemented, will raise an error

  • desired_meas_result (str) – Not currently implemented, will raise an error

  • save_mid_circuit_meas (bool) – Not currently implemented, will raise an error

Returns:
  • dict – A dictionary mapping multi-qubit states to their corresponding frequency.

  • numpy.array – The statevector, if available for the target backend and requested by the user (if not, set to None).

tangelo.linq.target.target_sympy module

class tangelo.linq.target.target_sympy.SympySimulator(n_shots=None, noise_model=None)

Bases: Backend

static backend_info()

A dictionary that includes {‘noisy_simulation’: True or False, ‘statevector_available’: True or False, ‘statevector_order’: ‘lsq_first’ or ‘msq_first’

expectation_value_from_prepared_state(qubit_operator, n_qubits, prepared_state=None)

Compute an expectation value using a representation of the state using sympy functionalities.

Parameters:
  • qubit_operator (QubitOperator) – a qubit operator in tangelo format

  • n_qubits (int) – Number of qubits.

  • prepared_state (array/matrix or sympy.physics.quantum.Qubit) – A numpy or a sympy object representing the state. Internally, a numpy object is transformed into the sympy representation. Default is None, in this case it is set to the current state in the simulator object.

Returns:

sympy.core.add.Add – Eigenvalue represented as a symbolic sum.

simulate_circuit(source_circuit: Circuit, return_statevector=False, initial_statevector=None, desired_meas_result=None, save_mid_circuit_meas=False)

This simulator manipulates symbolic expressions, i.e. gates can have unspecified parameters (strings interpreted as variables). As with the other simulators, it performs state preparation corresponding to the input circuit, returns the frequencies of the different observables, and either the statevector or None depending on if return_statevector is set to True.

Parameters:
  • source_circuit (Circuit) – A circuit in the abstract format to be translated for the target backend.

  • return_statevector (bool) – Option to return the statevector as well, if available.

  • initial_statevector (array/matrix or sympy.physics.quantum.Qubit) – A valid statevector in the format supported by the target backend.

  • desired_meas_result (str) – Not currently implemented, will raise an error

  • save_mid_circuit_meas (bool) – Not currently implemented, will raise an error

Returns:
  • dict – A dictionary mapping multi-qubit states to their corresponding frequency.

  • sympy.Matrix – The symbolic statevector, if requested by the user (if not, set to None).

Module contents