-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathq_circuit.py
48 lines (35 loc) · 1.94 KB
/
q_circuit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import q_library as qt
# THIS FILE SERVES TO MODIFY THE QUANTUM CIRCUIT (QUANTUM EMBEDDING KERNEL) USING LAYERS FROM THE Q_LIBRARY
# THE CURRENT ONE IS FROM Thomas Hubregtsen, David Wierichs, Elies Gil-Fuster, Peter-Jan H. S. Derks, Paul K. Faehrmann, and Johannes Jakob Meyer.
# “Training Quantum Embedding Kernels on Near-Term Quantum Computers.” arXiv:2105.02276, 2021. page 10
# THE ANSATZ (BLOCK), THE ADJOINT ANSATZ (ADJ_BLOCK) & THE FINAL QUANTUM CIRCUIT
# Blocks composed by layers which then create the circuit
# The initial state is passed by the blocks and layers to get the resulting final state of the circuit
# Blocks:
# - start is the feature with which to start the RZ-layer
# Circuit:
# - j and k the number of blocks
# - For the adjoint ansatz: the block parameters taken last first but themselves in right order -> reversed(params)
# ---
NUMBER_OF_BLOCK_PARAMS = 2 # results from the defined circuit, in this case: RY Layer and CRZ ring
def block(num_q, x, block_params, start, state):
state = qt.h_layer(num_q, state)
state = qt.rz_layer(num_q, x, state, start)
state = qt.ry_layer(num_q, block_params[0], state)
state = qt.crz_ring(num_q, block_params[1], state)
return state
def adj_block(num_q, x, block_params, start, state):
state = qt.adj_crz_ring(num_q, block_params[1], state)
state = qt.adj_ry_layer(num_q, block_params[0], state)
state = qt.adj_rz_layer(num_q, x, state, start)
state = qt.h_layer(num_q, state)
return state
# The Embedding Kernel of binary data (point1 and point2) in Quantum Space (resulting space)
def circuit(num_q, point1, point2, params):
state = qt.initial_state(num_q)
for j, block_params in enumerate(params):
state = block(num_q, point1, block_params, j * num_q, state)
for k, block_params in enumerate(reversed(params)):
state = adj_block(num_q, point2, block_params, (num_q-k-1) * num_q, state)
# print(state.grad_fn)
return state