2.4. Non-Unitary operations#
Besided the unitary operations, non-unitary operations are also an important part of geqo. Currently, the following non-unitary operators are supported by geqo:
Measure
DropQubits
SetBits
SetQubits
SetDensityMatrix
2.4.1. Measure#
This operation corresponds to a measurement of qubits in the standard basis and the results are stored in classical bits. The parameter of Measure
is the number of qubits, that are measured. This is equal to the number of classical bits to store the results.
In the following example, a Hadamard operation generates an equal superposition of both basis states \(|0\rangle\) and \(|1\rangle\) and after this operation the measurement on the qubit is performed.
The simulator ensembleSimulatorSymPy
keeps track of the measurement results and their corresponding probabilities and the resulting density matrices for each possible measurement result.
from geqo.simulators.sympy import ensembleSimulatorSymPy
from geqo.gates.fundamental_gates import Hadamard
from geqo.operations.measurement import Measure
gate = Hadamard() # Create a Hadamard gate for generating an equal superposition of both basis states of a qubit.
meas = Measure(
1
) # Create a measurement operation for one qubit; the result is stored in one classical bit.
sim = ensembleSimulatorSymPy(1, 1) # We need one qubit and one classical bit.
sim.apply(gate, [0]) # Generate the superposition of both states.
sim.apply(
meas, [0], [0]
) # The measurement is applied to the qubit and the result is stored in the classical bit.
sim.ensemble
{(0,): (1/2,
Matrix([
[1, 0],
[0, 0]])),
(1,): (1/2,
Matrix([
[0, 0],
[0, 1]]))}
2.4.2. DropQubits#
With this operation, one or more qubits can be dropped, i.e. the qubits are no longer part of the quantum state space after this operation. The dimension of the state space is reduced accordingly and the density matrix is traced out.
Note that the qubits and the order of all qubits stay in the list of qubits in a Sequence
or simulator to avoid problems with referring to qubits.
from geqo.simulators.sympy import ensembleSimulatorSymPy
from geqo.gates.fundamental_gates import Hadamard
from geqo.operations.measurement import DropQubits
gate = Hadamard()
drop = DropQubits(0)
sim = ensembleSimulatorSymPy(0, 1)
sim.apply(gate, [0])
sim.apply(drop, [0])
sim.ensemble
{(): (1, Matrix([[1]]))}
2.4.3. SetBits#
The operation SetBits
allows to set the values of classical bits. The first argument is a name for the bit values and the second argument is the number of classical bits, which the operation acts on.
For most simulators, the method setValue
must be used to assign a value to the settings of bits in the form of a list of 0 and 1 values.
from geqo.simulators.sympy import ensembleSimulatorSymPy
from geqo.gates.fundamental_gates import Hadamard
from geqo.initialization import SetBits
gate = Hadamard()
bits = SetBits("a", 2)
sim = ensembleSimulatorSymPy(2, 1)
sim.setValue("a", [1, 0])
sim.apply(gate, [0])
sim.apply(bits, [0, 1])
sim.ensemble
{(1, 0): (1,
Matrix([
[1/2, 1/2],
[1/2, 1/2]]))}
2.4.4. SetQubits#
The operation SetQubits
allows to set the values of qubits to \(|0\rangle\) and \(|1\rangle\). The first argument is a name for the qubit values and the second argument is the number of qubits, which the operation acts on.
For most simulators, the method setValue
must be used to assign a value to the settings of qubits in the form of a list of 0 and 1 values. The value \(0\) and \(1\) indicate whether a qubit is set to \(|0\rangle\) or \(|1\rangle\) in the standard basis.
from geqo.simulators.sympy import ensembleSimulatorSymPy
from geqo.gates.fundamental_gates import Hadamard
from geqo.initialization import SetQubits
qubits = SetQubits("a", 2)
sim = ensembleSimulatorSymPy(0, 2)
sim.setValue("a", [1, 0])
sim.apply(qubits, [0, 1])
sim.ensemble
{(): (1,
Matrix([
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 0]]))}
2.4.5. SetDensityMatrix#
This operator allows to set the density matrix of one or more qubits. Before the state is set, the affected qubits are traced out, leading to a mixed state in general. The first argument is a name for the density matrix and the second argument is the number of qubits, which the operation acts on.
For most simulators, the method setValue
must be used to assign a value to the density matrix. In the following example, the SymPy based simulator simulatorUnitarySymPy
is used and it supports symbolic values for the phase.
import sympy as sym
from geqo.simulators.sympy import ensembleSimulatorSymPy
from geqo.gates.fundamental_gates import Hadamard
from geqo.initialization import SetDensityMatrix
density = SetDensityMatrix("a", 1)
sim = ensembleSimulatorSymPy(0, 1)
sim.setValue("a", sym.Matrix([[1 / 2, 0], [0, 1 / 2]]))
sim.apply(density, [0])
sim.ensemble
{(): (1,
Matrix([
[0.5, 0],
[ 0, 0.5]]))}