## 输出算法操作

```from projectq import MainEngine
from projectq.cengines import DummyEngine
from projectq.ops import H, CX, All, Measure

backend = DummyEngine(save_commands=True)
eng = MainEngine(backend=backend)

qureg = eng.allocate_qureg(2)
H | qureg[0]
CX | (qureg[0], qureg[1])

All(Measure) | qureg
eng.flush(deallocate_qubits=True)

for cmd in backend.received_commands:
print (cmd)```

```Allocate | Qureg[0]
H | Qureg[0]
Allocate | Qureg[1]
CX | ( Qureg[0], Qureg[1] )
Measure | Qureg[0]
Measure | Qureg[1]
Deallocate | Qureg[0]
Deallocate | Qureg[1]```

## 封装的操作

```from projectq import MainEngine
from projectq.cengines import DummyEngine
from projectq.ops import H, CX, All, Measure, TimeEvolution, QubitOperator

backend = DummyEngine(save_commands=True)
eng = MainEngine(backend=backend)

qureg = eng.allocate_qureg(3)
H | qureg[0]
CX | (qureg[0], qureg[1])
TimeEvolution(1, QubitOperator("X2 X1")) | qureg

All(Measure) | qureg
eng.flush()

for cmd in backend.received_commands:
print (cmd)```

```Allocate | Qureg[0]
H | Qureg[0]
Allocate | Qureg[1]
CX | ( Qureg[0], Qureg[1] )
Measure | Qureg[0]
Allocate | Qureg[2]
exp(-1j * (1.0 X0 X1)) | Qureg[1-2]
Measure | Qureg[1]
Measure | Qureg[2]```

## 含时演化算符的分解

```from projectq import MainEngine
from projectq.cengines import DummyEngine
from projectq.ops import H, CX, All, Measure, TimeEvolution, QubitOperator
from projectq.setups import restrictedgateset

engine_list = restrictedgateset.get_engine_list(one_qubit_gates="any",two_qubit_gates=(CX,))
backend = DummyEngine(save_commands=True)
eng = MainEngine(backend=backend,engine_list=engine_list)

qureg = eng.allocate_qureg(3)
H | qureg[0]
CX | (qureg[0], qureg[1])
TimeEvolution(1, QubitOperator("X2 X1")) | qureg

All(Measure) | qureg
eng.flush(deallocate_qubits=True)

for cmd in backend.received_commands:
print (cmd)```

```Allocate | Qureg[0]
H | Qureg[0]
Allocate | Qureg[1]
CX | ( Qureg[0], Qureg[1] )
Measure | Qureg[0]
Allocate | Qureg[2]
H | Qureg[2]
H | Qureg[1]
CX | ( Qureg[1], Qureg[2] )
Rz(2.0) | Qureg[2]
CX | ( Qureg[1], Qureg[2] )
H | Qureg[1]
Measure | Qureg[1]
H | Qureg[2]
Measure | Qureg[2]
Deallocate | Qureg[0]
Deallocate | Qureg[1]
Deallocate | Qureg[2]```

## QFT的分解

QFT是ProjectQ中所自带支持的量子傅里叶变换的量子门操作封装，跟上一个章节中所介绍的含时演化算符类似的，我们可以用`restrictedgateset`来具体分解QFT算符：

```from projectq import MainEngine
from projectq.cengines import DummyEngine
from projectq.ops import H, CX, All, Measure, TimeEvolution, QubitOperator, QFT
from projectq.setups import restrictedgateset

engine_list = restrictedgateset.get_engine_list(one_qubit_gates="any",two_qubit_gates=(CX,))
backend = DummyEngine(save_commands=True)
eng = MainEngine(backend=backend,engine_list=engine_list)

qureg = eng.allocate_qureg(3)
H | qureg[0]
CX | (qureg[0], qureg[1])
QFT | qureg

All(Measure) | qureg
eng.flush(deallocate_qubits=True)

for cmd in backend.received_commands:
print (cmd)```

```Allocate | Qureg[2]
Allocate | Qureg[1]
H | Qureg[2]
Rz(0.785398163398) | Qureg[2]
Allocate | Qureg[0]
H | Qureg[0]
CX | ( Qureg[0], Qureg[1] )
R(0.785398163398) | Qureg[1]
CX | ( Qureg[1], Qureg[2] )
Rz(11.780972450962) | Qureg[2]
CX | ( Qureg[1], Qureg[2] )
R(0.392699081698) | Qureg[0]
Rz(0.392699081698) | Qureg[2]
CX | ( Qureg[0], Qureg[2] )
H | Qureg[1]
Rz(12.173671532661) | Qureg[2]
CX | ( Qureg[0], Qureg[2] )
R(0.785398163398) | Qureg[0]
Rz(0.785398163398) | Qureg[1]
CX | ( Qureg[0], Qureg[1] )
Rz(11.780972450962) | Qureg[1]
CX | ( Qureg[0], Qureg[1] )
H | Qureg[0]
Measure | Qureg[0]
Measure | Qureg[1]
Measure | Qureg[2]
Deallocate | Qureg[1]
Deallocate | Qureg[2]
Deallocate | Qureg[0]```

```from projectq import MainEngine
from projectq.cengines import DummyEngine
from projectq.ops import H, CX, All, Measure, TimeEvolution, QubitOperator, QFT
from projectq.setups import restrictedgateset

engine_list = restrictedgateset.get_engine_list(one_qubit_gates="any",two_qubit_gates="any")
backend = DummyEngine(save_commands=True)
eng = MainEngine(backend=backend,engine_list=engine_list)

qureg = eng.allocate_qureg(3)
H | qureg[0]
CX | (qureg[0], qureg[1])
QFT | qureg

All(Measure) | qureg
eng.flush(deallocate_qubits=True)

for cmd in backend.received_commands:
print (cmd)```

```Allocate | Qureg[0]
Allocate | Qureg[1]
H | Qureg[0]
CX | ( Qureg[0], Qureg[1] )
Allocate | Qureg[2]
H | Qureg[2]
CR(1.570796326795) | ( Qureg[1], Qureg[2] )
CR(0.785398163397) | ( Qureg[0], Qureg[2] )
H | Qureg[1]
CR(1.570796326795) | ( Qureg[0], Qureg[1] )
H | Qureg[0]
Measure | Qureg[0]
Measure | Qureg[1]
Measure | Qureg[2]
Deallocate | Qureg[1]
Deallocate | Qureg[2]
Deallocate | Qureg[0]```