setups.decompositions¶
The decomposition package is a collection of gate decomposition / replacement rules which can be used by, e.g., the AutoReplacer engine.
projectq.setups.decompositions.amplitudeamplification |
Registers a decomposition for quantum amplitude amplification. |
projectq.setups.decompositions.arb1qubit2rzandry |
Registers the Z-Y decomposition for an arbitrary one qubit gate. |
projectq.setups.decompositions.barrier |
Registers a decomposition rule for barriers. |
projectq.setups.decompositions.carb1qubit2cnotrzandry |
Registers the decomposition of an controlled arbitary single qubit gate. |
projectq.setups.decompositions.cnot2cz |
Registers a decomposition to for a CNOT gate in terms of CZ and Hadamard. |
projectq.setups.decompositions.cnot2rxx |
Registers a decomposition to for a CNOT gate in terms of Rxx, Rx and Ry gates. |
projectq.setups.decompositions.cnu2toffoliandcu |
Registers a decomposition rule for multi-controlled gates. |
projectq.setups.decompositions.crz2cxandrz |
Registers a decomposition for controlled z-rotation gates. |
projectq.setups.decompositions.entangle |
Registers a decomposition for the Entangle gate. |
projectq.setups.decompositions.globalphase |
Registers a decomposition rule for global phases. |
projectq.setups.decompositions.h2rx |
Registers a decomposition for the H gate into an Ry and Rx gate. |
projectq.setups.decompositions.module |
Registers a decomposition for quantum amplitude amplification. |
projectq.setups.decompositions.ph2r |
Registers a decomposition for the controlled global phase gate. |
projectq.setups.decompositions.phaseestimation |
Registers a decomposition for phase estimation. |
projectq.setups.decompositions.qft2crandhadamard |
Registers a decomposition rule for the quantum Fourier transform. |
projectq.setups.decompositions.qubitop2onequbit |
Registers a decomposition rule for a unitary QubitOperator to one qubit gates. |
projectq.setups.decompositions.r2rzandph |
Registers a decomposition rule for the phase-shift gate. |
projectq.setups.decompositions.rx2rz |
Registers a decomposition for the Rx gate into an Rz gate and Hadamard. |
projectq.setups.decompositions.ry2rz |
Registers a decomposition for the Ry gate into an Rz and Rx(pi/2) gate. |
projectq.setups.decompositions.rz2rx |
Registers a decomposition for the Rz gate into an Rx and Ry(pi/2) or Ry(-pi/2) gate |
projectq.setups.decompositions.sqrtswap2cnot |
Registers a decomposition to achieve a SqrtSwap gate. |
projectq.setups.decompositions.stateprep2cnot |
Registers decomposition for StatePreparation. |
projectq.setups.decompositions.swap2cnot |
Registers a decomposition to achieve a Swap gate. |
projectq.setups.decompositions.time_evolution |
Registers decomposition for the TimeEvolution gates. |
projectq.setups.decompositions.toffoli2cnotandtgate |
Registers a decomposition rule for the Toffoli gate. |
projectq.setups.decompositions.uniformlycontrolledr2cnot |
Registers decomposition for UnformlyControlledRy and UnformlyControlledRz. |
projectq.setups.decompositions.all_defined_decomposition_rules |
list() -> new empty list list(iterable) -> new list initialized from iterable’s items |
Submodules¶
amplitudeamplification¶
Registers a decomposition for quantum amplitude amplification.
(Quick reference https://en.wikipedia.org/wiki/Amplitude_amplification. Complete reference G. Brassard, P. Hoyer, M. Mosca, A. Tapp (2000) Quantum Amplitude Amplification and Estimation https://arxiv.org/abs/quant-ph/0005055)
Quantum Amplitude Amplification (QAA) executes the algorithm, but not the final measurement required to obtain the marked state(s) with high probability. The starting state on wich the QAA algorithm is executed is the one resulting of aplying the Algorithm on the |0> state.
Example
def func_algorithm(eng,system_qubits):
All(H) | system_qubits
def func_oracle(eng,system_qubits,qaa_ancilla):
# This oracle selects the state |010> as the one marked
with Compute(eng):
All(X) | system_qubits[0::2]
with Control(eng, system_qubits):
X | qaa_ancilla
Uncompute(eng)
system_qubits = eng.allocate_qureg(3)
# Prepare the qaa_ancilla qubit in the |-> state
qaa_ancilla = eng.allocate_qubit()
X | qaa_ancilla
H | qaa_ancilla
# Creates the initial state form the Algorithm
func_algorithm(eng, system_qubits)
# Apply Quantum Amplitude Amplification the correct number of times
num_it = int(math.pi/4.*math.sqrt(1 << 3))
with Loop(eng, num_it):
QAA(func_algorithm, func_oracle) | (system_qubits, qaa_ancilla)
All(Measure) | system_qubits
Warning
No qubit allocation/deallocation may take place during the call
to the defined Algorithm func_algorithm
-
projectq.setups.decompositions.amplitudeamplification.
func_algorithm
¶ Algorithm that initialite the state and to be used in the QAA algorithm
-
projectq.setups.decompositions.amplitudeamplification.
func_oracle
¶ The Oracle that marks the state(s) as “good”
-
projectq.setups.decompositions.amplitudeamplification.
system_qubits
¶ the system we are interested on
-
projectq.setups.decompositions.amplitudeamplification.
qaa_ancilla
¶ auxiliary qubit that helps to invert the amplitude of the “good” states
arb1qubit2rzandry¶
Registers the Z-Y decomposition for an arbitrary one qubit gate.
See paper “Elementary gates for quantum computing” by Adriano Barenco et al., arXiv:quant-ph/9503016v1. (Note: They use different gate definitions!) Or see theorem 4.1 in Nielsen and Chuang.
Decompose an arbitrary one qubit gate U into U = e^(i alpha) Rz(beta) Ry(gamma) Rz(delta). If a gate V is element of SU(2), i.e., determinant == 1, then V = Rz(beta) Ry(gamma) Rz(delta)
barrier¶
Registers a decomposition rule for barriers.
Deletes all barriers if they are not supported.
carb1qubit2cnotrzandry¶
Registers the decomposition of an controlled arbitary single qubit gate.
See paper “Elementary gates for quantum computing” by Adriano Barenco et al., arXiv:quant-ph/9503016v1. (Note: They use different gate definitions!) or Nielsen and Chuang chapter 4.3.
cnot2cz¶
Registers a decomposition to for a CNOT gate in terms of CZ and Hadamard.
cnot2rxx¶
Registers a decomposition to for a CNOT gate in terms of Rxx, Rx and Ry gates.
cnu2toffoliandcu¶
Registers a decomposition rule for multi-controlled gates.
Implements the decomposition of Nielsen and Chuang (Fig. 4.10) which decomposes a C^n(U) gate into a sequence of 2 * (n-1) Toffoli gates and one C(U) gate by using (n-1) ancilla qubits and circuit depth of 2n-1.
crz2cxandrz¶
Registers a decomposition for controlled z-rotation gates.
It uses 2 z-rotations and 2 C^n NOT gates to achieve this gate.
entangle¶
Registers a decomposition for the Entangle gate.
Applies a Hadamard gate to the first qubit and then, conditioned on this first qubit, CNOT gates to all others.
globalphase¶
Registers a decomposition rule for global phases.
Deletes global phase gates (which can be ignored).
h2rx¶
Registers a decomposition for the H gate into an Ry and Rx gate.
module¶
Registers a decomposition for quantum amplitude amplification.
(Quick reference https://en.wikipedia.org/wiki/Amplitude_amplification. Complete reference G. Brassard, P. Hoyer, M. Mosca, A. Tapp (2000) Quantum Amplitude Amplification and Estimation https://arxiv.org/abs/quant-ph/0005055)
Quantum Amplitude Amplification (QAA) executes the algorithm, but not the final measurement required to obtain the marked state(s) with high probability. The starting state on wich the QAA algorithm is executed is the one resulting of aplying the Algorithm on the |0> state.
Example
def func_algorithm(eng,system_qubits):
All(H) | system_qubits
def func_oracle(eng,system_qubits,qaa_ancilla):
# This oracle selects the state |010> as the one marked
with Compute(eng):
All(X) | system_qubits[0::2]
with Control(eng, system_qubits):
X | qaa_ancilla
Uncompute(eng)
system_qubits = eng.allocate_qureg(3)
# Prepare the qaa_ancilla qubit in the |-> state
qaa_ancilla = eng.allocate_qubit()
X | qaa_ancilla
H | qaa_ancilla
# Creates the initial state form the Algorithm
func_algorithm(eng, system_qubits)
# Apply Quantum Amplitude Amplification the correct number of times
num_it = int(math.pi/4.*math.sqrt(1 << 3))
with Loop(eng, num_it):
QAA(func_algorithm, func_oracle) | (system_qubits, qaa_ancilla)
All(Measure) | system_qubits
Warning
No qubit allocation/deallocation may take place during the call
to the defined Algorithm func_algorithm
-
projectq.setups.decompositions.module.
func_algorithm
¶ Algorithm that initialite the state and to be used in the QAA algorithm
-
projectq.setups.decompositions.module.
func_oracle
¶ The Oracle that marks the state(s) as “good”
-
projectq.setups.decompositions.module.
system_qubits
¶ the system we are interested on
-
projectq.setups.decompositions.module.
qaa_ancilla
¶ auxiliary qubit that helps to invert the amplitude of the “good” states
ph2r¶
Registers a decomposition for the controlled global phase gate.
Turns the controlled global phase gate into a (controlled) phase-shift gate. Each time this rule is applied, one control can be shaved off.
phaseestimation¶
Registers a decomposition for phase estimation.
(reference https://en.wikipedia.org/wiki/Quantum_phase_estimation_algorithm)
The Quantum Phase Estimation (QPE) executes the algorithm up to the inverse QFT included. The following steps measuring the ancillas and computing the phase should be executed outside of the QPE.
The decomposition uses as ancillas (qpe_ancillas) the first qubit/qureg in the Command and as system qubits teh second qubit/qureg in the Command.
The unitary operator for which the phase estimation is estimated (unitary) is the gate in Command
Example
# Example using a ProjectQ gate
n_qpe_ancillas = 3
qpe_ancillas = eng.allocate_qureg(n_qpe_ancillas)
system_qubits = eng.allocate_qureg(1)
angle = cmath.pi*2.*0.125
U = Ph(angle) # unitary_specfic_to_the_problem()
# Apply Quantum Phase Estimation
QPE(U) | (qpe_ancillas, system_qubits)
All(Measure) | qpe_ancillas
# Compute the phase from the ancilla measurement
#(https://en.wikipedia.org/wiki/Quantum_phase_estimation_algorithm)
phasebinlist = [int(q) for q in qpe_ancillas]
phase_in_bin = ''.join(str(j) for j in phasebinlist)
phase_int = int(phase_in_bin,2)
phase = phase_int / (2 ** n_qpe_ancillas)
print (phase)
# Example using a function (two_qubit_gate).
# Instead of applying QPE on a gate U one could provide a function
def two_qubit_gate(system_q, time):
CNOT | (system_q[0], system_q[1])
Ph(2.0*cmath.pi*(time * 0.125)) | system_q[1]
CNOT | (system_q[0], system_q[1])
n_qpe_ancillas = 3
qpe_ancillas = eng.allocate_qureg(n_qpe_ancillas)
system_qubits = eng.allocate_qureg(2)
X | system_qubits[0]
# Apply Quantum Phase Estimation
QPE(two_qubit_gate) | (qpe_ancillas, system_qubits)
All(Measure) | qpe_ancillas
# Compute the phase from the ancilla measurement
#(https://en.wikipedia.org/wiki/Quantum_phase_estimation_algorithm)
phasebinlist = [int(q) for q in qpe_ancillas]
phase_in_bin = ''.join(str(j) for j in phasebinlist)
phase_int = int(phase_in_bin,2)
phase = phase_int / (2 ** n_qpe_ancillas)
print (phase)
-
projectq.setups.decompositions.phaseestimation.
unitary
¶ Unitary Operation either a ProjectQ gate or a function f.
Type: BasicGate
-
Calling the function with the parameters system_qubits
Type: Qureg) and time (integer
-
i.e. f
Type: system_qubits, time
-
with parameter time.
qft2crandhadamard¶
Registers a decomposition rule for the quantum Fourier transform.
Decomposes the QFT gate into Hadamard and controlled phase-shift gates (R).
Warning
The final Swaps are not included, as those are simply a re-indexing of quantum registers.
qubitop2onequbit¶
Registers a decomposition rule for a unitary QubitOperator to one qubit gates.
r2rzandph¶
Registers a decomposition rule for the phase-shift gate.
Decomposes the (controlled) phase-shift gate using z-rotation and a global phase gate.
rx2rz¶
Registers a decomposition for the Rx gate into an Rz gate and Hadamard.
ry2rz¶
Registers a decomposition for the Ry gate into an Rz and Rx(pi/2) gate.
rz2rx¶
Registers a decomposition for the Rz gate into an Rx and Ry(pi/2) or Ry(-pi/2) gate
sqrtswap2cnot¶
Registers a decomposition to achieve a SqrtSwap gate.
stateprep2cnot¶
Registers decomposition for StatePreparation.
swap2cnot¶
Registers a decomposition to achieve a Swap gate.
Decomposes a Swap gate using 3 CNOT gates, where the one in the middle features as many control qubits as the Swap gate has control qubits.
time_evolution¶
Registers decomposition for the TimeEvolution gates.
An exact straight forward decomposition of a TimeEvolution gate is possible if the hamiltonian has only one term or if all the terms commute with each other in which case one can implement each term individually.
toffoli2cnotandtgate¶
Registers a decomposition rule for the Toffoli gate.
Decomposes the Toffoli gate using Hadamard, T, Tdag, and CNOT gates.
uniformlycontrolledr2cnot¶
Registers decomposition for UnformlyControlledRy and UnformlyControlledRz.