quantumaudio package

quantumaudio: A Python class implementation for Quantum Representations of Audio in Qiskit. Developed by the quantum computer music team at the Interdisciplinary Centre for Computer Music Research, University of Plymouth, UK

quantumaudio module contents

class EncodingScheme[source]

Bases: object

get_encoder(encoder_name: str) AnyEncoder[source]

Returns: encoder class associated with name.

class QPAM[source]

Bases: object

convert(original_audio: ndarray[Any, dtype[ScalarType]]) ndarray[Any, dtype[ScalarType]][source]

Converts the digital audio into an array of probability amplitudes.

The audio signal is normalized. The normalized samples can then be interpreted as probability amplitudes. In other words, by squaring every sample, their total sum is now 1.

Parameters:

original_audio – Numpy Array containing audio information.

Returns:

A Numpy Array containing normalized probability amplitudes.

measure(qc: QuantumCircuit, treg_pos: int = 0) None[source]

Appends Measurements to a QPAM audio circuit

From a quantum circuit with a register containing a QPAM representation of quantum audio, creates a classical register with compatible size and adds isntructions for measuring the QPAM register.

Parameters:
  • qc – A qiskit quantum circuit containing at least 1 quantum register.

  • treg_pos – Index of the QPAM (‘treg’) register in the circuit. Default is 0

prepare(audio_amplitudes: ndarray[Any, dtype[ScalarType]], regsize: Tuple[int, int], regnames: Tuple[str, str], print_state: bool = False) QuantumCircuit[source]

Prepares a QPAM quantum circuit.

Creates a qiskit QuantumCircuit that prepares a Quantum Audio state using QPAM (Quantum Probability Amplitude Modulation) representation. The quantum circuits used for audio representations typically contain two qubit registers, namely, ‘treg’ (which encodes time/index information) and ‘areg’ (which encodes amplitude information).

Note: In QPAM, the ‘areg’ (amplitude) register is NOT used as the amplitude information is encoded in the probability amplitudes of the ‘treg’ (time) register.

Parameters:
  • audio_amplitudes – Array with propbability amplitudes

  • regsize – The size of both qubit registers in a tuple (treg_size, areg_size). ‘treg_size’ qubits for ‘treg’; ‘areg_size’ qubits for ‘areg’. For QPAM, ‘areg_size’ is ALWAYS 0

  • regnames – Label names for ‘treg’ and ‘areg’, passed as a tuple. For visualization purposes only.

  • print_state – Toggles a simple print of the prepared quantum state to the console, for visualization purposes only.

Returns:

A qiskit QuantumCircuit with specific QPAM preparation instructions.

reconstruct(treg_size: int, counts: Counts, shots: int, g: float | None = None) ndarray[Any, dtype[ScalarType]][source]

Builds a digital Audio from qiskit histogram data.

Considering the QPAM encoding scheme, it uses the histogram data stored in a Counts object (qiskit.result.Counts) to reconstruct an audio signal. It renormalizes the histogram counts and remaps the signal back to the [-1 to 1] range.

Parameters:
  • treg_size – Size of the ‘treg’ (time) register.

  • counts – Histogram from a qiskit job result (result.get_counts())

  • shots – Amount of identical experiments ran by the qiskit job.

  • g – Gain factor. This is a renormalization factor. (When bypassing audio signals through quantum circuits, this factor is usually proportional to the origal audio’s norm).

Returns:

A Digital Audio as a Numpy Array. The signal is in float format.

class QSM[source]

Bases: object

convert(original_audio)[source]

For the QSM encoding scheme, this function is dummy.

QSM expects a quantized signal (N-Bit PCM) as input. No pre-processing is needed after this point.

measure(qc: QuantumCircuit, treg_pos: int = 1, areg_pos: int = 0) None[source]

Appends Measurements to a QSM audio circuit

From a quantum circuit with registers containing a QSM representation of quantum audio, creates two classical registers with compatible sizes and adds instructions for measuring them.

Parameters:
  • qc – A quantum circuit containing at least 2 quantum registers.

  • treg_pos – Index of the SQPAM (‘treg’) register in the circuit. Default is 1

  • areg_pos – Index of the SQPAM (‘areg’) register in the circuit. Default is 0

omega_t(qa: QuantumCircuit, t: int, a: int, treg: QuantumRegister, areg: QuantumRegister, print_state: bool = False) None[source]

QSM Value-Setting operation.

Applies a multi-controlled CNOT gate to qubits of amplitude register, controlled by the time register at the respective time index state. In other words. At index ‘t’, it flipps the amplitude qubits to match the original audio sample bits at the same index.

Parameters:
  • qa – The quantum circuit to be manipulated.

  • t – Time index that will be encoded.

  • a – Quantized sample from original audio to be converted to binary.

  • treg – Time register, ‘treg’.

  • areg – Amplitude Register, ‘areg’.

  • print_state – Toggles a simple print of the prepared quantum state to the console, for visualization purposes only. To be used together with all other SQPAM methods with a ‘print_state’ kwarg.

prepare(quantized_audio: ndarray[Any, dtype[ScalarType]], regsize: Tuple[int, int], regnames: Tuple[str, str], print_state: bool = False) QuantumCircuit[source]

Prepares a QSM quantum circuit.

Creates a qiskit QuantumCircuit that prepares a Quantum Audio state using QSM (Quantum State Modulation). The quantum circuits used for audio representations typically contain two qubit registers, namely, ‘treg’ (which encodes time/index information) and ‘areg’ (which encodes amplitude information).

Parameters:
  • quantized_audio – Integer Array with the input signal.

  • regsize – The size of both qubit registers in a tuple (treg_size, areg_size). ‘treg_size’ qubits for ‘treg’; ‘areg_size’ qubits for ‘areg’.

  • regnames – Label names for ‘treg’ and ‘areg’, passed as a tuple. For visualization purposes only.

  • print_state – Toggles a simple print of the prepared quantum state to the console, for visualization purposes only.

Returns:

A qiskit quantum circuit containing specific QSM preparation instructions.

reconstruct(treg_size: int, counts: Counts) ndarray[Any, dtype[ScalarType]][source]

Builds a digital Audio from qiskit histogram data.

Considering the QSM encoding scheme, it uses the histogram data stored in a Counts object (qiskit.result.counts.Counts) to reconstruct an audio signal. It uses the bin labels of the histogram, which contains the measured quantum states in binary form. It converts the binary pairs to (amplitude, index) pairs, building an Array.

Parameters:
  • treg_size – Size of the ‘treg’ (time) register.

  • counts – Histogram from a qiskit job result (result.get_counts())

Returns:

A Digital Audio as a Numpy Array. The signal is in quantized (int) format.

treg_index_X(qc: QuantumCircuit, t: int, treg: QuantumRegister, print_state: bool = False) None[source]

Auxilary function for matching control conditions with time indexes.

Applies X gates on qubits of the time register whenever the respective bit of the current time index (in binary representation) is 0. As a result, the qubit will be flipped to |1> and succesfully trigger necessary control conditions of the circuit for this time index.

Parameters:
  • qa – The quantum circuit to be maniputated

  • t – Time index that will be converted to binary form for comparison.

  • treg – Quantum register of the time indexes.

  • print_state – Toggles a simple print of the prepared quantum state to the console, for visualization purposes only. To be used together with all other QSM methods with a ‘print_state’ kwarg.

Examples

treg_index_X(qa, 6, treg) (a time register ‘treg’ with, say, 5 qubits in ‘qa’ at instant 6)

‘t’ = 6 == ‘00110’. treg_index_X() applies X gates to qubits 0, 3 and 4

(right to left) of register ‘treg’.

class QuantumAudio(encoder_name: str)[source]

Bases: object

listen(rate: int = 44100) None[source]

Plays the audio file using ipython.display.Audio()

load_input(input_audio: ndarray[Any, dtype[floating]], bit_depth: int = 1) QuantumAudio[source]

Loads an audio file and calculates the qubit requirements.

Brings a digital audio signal inside the class for further processing. Audio files should be in numpy.ndarray type and be in the (-1. to 1.) amplitude range. You can also optionally load a quantized audio signal as input (-N to N-1) range, as long as you specify the bit depth of your quantized input ‘areg_size’

Parameters:
  • input_audio – The audio signal to be converted. If not in 32-bit or 64-bit float format (‘n’-bit integer PCM), specify bit depth.

  • bit_depth – Audio bit depth IF using integer PCM. Ignore otherwise.

Returns:

Returns itself for using multiple QuantumAudio methods in one line of code.

Examples

>>> float_audio = [0., -0.25, 0.5 , 0.75,  -0.75  ,  -1.,  0.25]
>>> quantum_audio = qa.QuantumAudio('qpam').load_input(float_audio)
For this input, the QPAM representation will require:
        3 qubits for encoding time information and
        0 qubits for encoding ampĺitude information.
>>> int_3bit_PCM_audio = [0, -1, 2, 3, -3, -4, 1]
>>> quantum_audio = qa.QuantumAudio('qsm').load_input(int_3bit_PCM_audio, 3)
For this input, the QSM representation will require:
        3 qubits for encoding time information and
        3 qubits for encoding ampĺitude information.
measure(treg_pos: int | None = None, areg_pos: int | None = None) QuantumAudio[source]

Updates quantum circuit by adding measurements in the end.

Will add a measurement instruction to the end of each qubit register.

Returns:

Returns itself for using multiple QuantumAudio methods in one line of code.

plot_audio() None[source]

Plots comparisons between the input and output audio files.

Uses matplotlib.

prepare(tregname: str = 't', aregname: str = 'a', print_state: bool = False) QuantumAudio[source]

Creates a Quantum Circuit that prepares the audio representation.

Loads the ‘circuit’ attribute with the preparation circuit, according to the encoding technique used: QPAM, SQPAM or QSM.

Returns:

Returns itself for using multiple QuantumAudio methods in one line of code.

reconstruct_audio(**additional_kwargs: Any) QuantumAudio[source]

Builds an audio signal from a qiskit result histogram.

Depending on the chosen encoding technique, reconstructs an audio file using the histogram in QuantumAudio.counts (qiskit.result.Counts)

Returns:

Returns itself for using multiple QuantumAudio methods in one line of code.

run(shots: int = 10, backend_name: str = 'aer_simulator', provider=<qiskit_aer.aerprovider.AerProvider object>) QuantumAudio[source]

Runs the Quantum Circuit in an IBMQ job.

Transpiles and runs QuantumAudio.circuit in a qiskit job. Supports IBMQ remote backends.

Returns:

Returns itself for using multiple QuantumAudio methods in one line of code.

class SQPAM[source]

Bases: object

convert(original_audio: ndarray[Any, dtype[ScalarType]]) ndarray[Any, dtype[ScalarType]][source]

Converts digital audio into an array of probability amplitudes.

The audio signal is mapped to an array of angles. The angles can then be interpreted as real-valued parameters for a trigonometric representation subspace of a qubit. In other words, the angles are used to rotate a qubit - originally in the |0> state - to the following state: ( cos(angle)|0> + sin(angle)|1> ). Notice that this preserves probabilities, as cos^2 + sin^2 = 1.

Note: By convention, we are using the np.arcsin function to calculate the angles. This means that the SQPAM.reconstruct() method will use the even (sine) bins of the histogram to retrieve the signal.

Parameters:

original_audio – Numpy Array containing audio information.

Returns:

A Numpy Array containing angles between 0 and pi/2.

mc_Ry_2theta_t(qa: QuantumCircuit, t: int, a: float, treg: QuantumRegister, areg: QuantumRegister, print_state: bool = False) None[source]

SQPAM Value-Setting operation.

Applies a controlled Ry(2*theta) gate to the amplitude register, controlled by the time register at the respective time index state. In other words. At index ‘t’, it rotates the aplitude qubit by the angle mapped from the audio sample at the same index. In quantum computing terms, this translates to a multi-controlled rotation gate.

Parameters:
  • qa – The quantum circuit to be manipulated.

  • t – Time index that will be encoded.

  • a – Angle of rotation.

  • treg – Time register, ‘treg’.

  • areg – Amplitude Register, ‘areg’.

  • print_state – Toggles a simple print of the prepared quantum state to the console, for visualization purposes only. To be used together with all other SQPAM methods with a ‘print_state’ kwarg.

measure(qc: QuantumCircuit, treg_pos: int = 1, areg_pos: int = 0) None[source]

Appends Measurements to an SQPAM audio circuit

From a quantum circuit with registers containing an SQPAM representation of quantum audio, creates two classical registers with compatible sizes and adds instructions for measuring them.

Parameters:
  • qc – A quantum circuit containing at least 2 quantum registers.

  • treg_pos – Index of the SQPAM (‘treg’) register in the circuit. Default is 1

  • areg_pos – Index of the SQPAM (‘areg’) register in the circuit. Default is 0

prepare(angles: ndarray[Any, dtype[ScalarType]], regsize: Tuple[int, int], regnames: Tuple[str, str], print_state: bool = False) QuantumCircuit[source]

Prepares an SQPAM quantum circuit.

Creates a qiskit QuantumCircuit that prepares a Quantum Audio state using SQPAM (Single-Qubit Probability Amplitude Modulation). The quantum circuits used for audio representations typically contain two qubit registers, namely, ‘treg’ (which encodes time/index information) and ‘areg’ (which encodes amplitude information).

Note: In SQPAM (as hinted by its name), the ‘areg’ (amplitude) register contains a single qubit. The audio samples are mapped into angles that parametrize single qubit rotations of ‘areg’ - which are then correlated to index states of the ‘treg’ register.

Parameters:
  • angles – Array with propbability amplitudes

  • regsize – The size of both qubit registers in a tuple (treg_size, areg_size). ‘treg_size’ qubits for ‘treg’; ‘areg_size’ qubits for ‘areg’. For SQPAM, ‘areg_size’ is ALWAYS 1

  • regnames – Label names for ‘treg’ and ‘areg’, passed as a tuple. For visualization purposes only.

  • print_state – Toggles a simple print of the prepared quantum state to the console, for visualization purposes only.

Returns:

A qiskit quantum circuit containing specific SQPAM preparation instructions.

reconstruct(treg_size: int, counts: Counts, shots: int, inverted: bool = False, both: bool = False) ndarray[Any, dtype[ScalarType]][source]

Builds a digital Audio from qiskit histogram data.

Considering the SQPAM encoding scheme, it uses the histogram data stored in a Counts object (qiskit.result.Counts) to reconstruct an audio signal. It separates the even bins (sine coefficients) from the odd bins (cosine coefficients) of the histogram. Since the SQPAM.convert() method used the np.arcsin() function to prepare the state, the even bins should be used for reconstructing the signal.

However, the relations between sine and cosine means that a reconstruction with the cosine terms will build a perfectly inverted version of the signal. The user is able to choose between retrieving original or phase-inverted (or both) signals.

Parameters:
  • treg_size – Size of the ‘treg’ (time) register, leading to the full audio size.

  • counts – Histogram from a qiskit job result (result.get_counts())

  • shots – Amount of identical experiments ran by the qiskit job.

  • inverted – Retrieves the cosine amplitudes instead (leading to a phase-inverted version of the signal).

  • both – Retrieves both Sine and Cosine amplitudes in a tuple. Overwrites the ‘inverted’ argument.

Returns:

A Digital Audio as a Numpy Array, or a Tuple with two signals. The signals are in float format.

treg_index_X(qa: QuantumCircuit, t: int, treg: QuantumRegister, print_state: bool = False) None[source]

Auxilary function for matching control conditions with time indexes.

Applies X gates on qubits of the time register whenever the respective bit of the current time index (in binary representation) is 0. As a result, the qubit will be flipped to |1> and succesfully trigger necessary control conditions of the circuit for this time index.

Parameters:
  • qa – The quantum circuit to be maniputated

  • t – Time index that will be converted to binary form for comparison.

  • treg – Quantum register of the time indexes.

  • print_state – Toggles a simple print of the prepared quantum state to the console, for visualization purposes only. To be used together with all other SQPAM methods with a ‘print_state’ kwarg.

Examples

treg_index_X(qa, 6, treg) (a time register ‘treg’ with, say, 5 qubits in ‘qa’ at instant 6)

‘t’ = 6 == ‘00110’. treg_index_X() applies X gates to qubits 0, 3 and 4

(right to left) of register ‘treg’.

requantize_input(audio: ndarray[Any, dtype[ScalarType]], bit_depth: int) ndarray[Any, dtype[ScalarType]][source]

Requantizes Array signals and PCM audio signals.

Utilitary Function for downsizing the bit depth of an audio file. Very useful for using with the QSM encoder ‘QuantumAudio(‘qsm’)’.

Returns:

(Numpy Array) Requantized audio signal.