-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathmaxkcut_binary_fullH.py
More file actions
693 lines (649 loc) · 31 KB
/
Copy pathmaxkcut_binary_fullH.py
File metadata and controls
693 lines (649 loc) · 31 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
import networkx as nx
import numpy as np
import itertools
from qiskit import QuantumCircuit, QuantumRegister, AncillaRegister
from qiskit.circuit import Parameter
from qiskit.circuit.library import PhaseGate
from qiskit.circuit.library import PauliEvolutionGate
from qiskit.quantum_info import SparsePauliOp, Pauli
from .graph_problem import GraphProblem
from .maxkcut_binary_powertwo import MaxKCutBinaryPowerOfTwo
class MaxKCutBinaryFullH(GraphProblem):
"""
Max k-CUT Binary Full H graph problem.
Subclass of the `GraphProblem` class. Implements the Max k-Cut problem using a binary encoding and full Hamiltonian construction for QAOA.
Mainly used when k not a power of two and excess states must be grouped together to represent the same color.
This class supports several encoding and circuit construction methods for different values of k, and allows for
flexible color encodings and optional node fixing.
Attributes:
G (nx.Graph): The input graph on which the Max k-Cut problem is defined.
k_cuts (int): The number of partitions (colors) to cut the graph into.
color_encoding (str): The encoding scheme for colors, e.g. "LessThanK" or "max_balanced".
method (str): The method used for circuit construction, one of "PauliBasis", "PowerOfTwo" or "Diffusion".
fix_one_node (bool): If True, fixes the last node to a specific color, reducing the number of variables.
N_qubits_per_node (int): Number of qubits used to encode each node.
colors (dict): Maps color labels to lists of binary strings representing that color.
bitstring_to_color (dict): Maps each binary string to its corresponding color label.
mkcb_pot (MaxKCutBinaryPowerOfTwo): Helper instance for the "PowerOfTwo" method (if used).
N_ancilla_qubits (int): Number of ancilla qubits required for the "PowerOfTwo" method.
op (SparsePauliOp): The full Pauli operator for the cost Hamiltonian (if using "PauliBasis").
ophalf (SparsePauliOp): The half Pauli operator for the fixed-node case (if using "PauliBasis").
Methods:
validate_parameters(k, method, fix_one_node): Validates the input parameters for k, method, and fix_one_node.
construct_colors(): Constructs the mapping from binary strings to color classes based on k and encoding.
apply_N(circuit, binary_str1, binary_str2): Applies X gates to the circuit to map between two binary color encodings.
add_equalize_color(qc, bs1, bs2, theta): Adds gates to the circuit to equalize the phase between two color encodings.
create_edge_circuit(theta): Creates the parameterized quantum circuit for an edge, according to the chosen method.
create_edge_circuit_fixed_node(theta): Creates the parameterized quantum circuit for an edge when one node is fixed.
getPauliOperator(k_cuts, color_encoding): Returns the Pauli operators for the cost Hamiltonian for the given k and encoding.
"""
def __init__(
self,
G: nx.Graph,
k_cuts: int,
color_encoding: str,
method: str = "Diffusion",
fix_one_node: bool = False, # this fixes the last node to color 1, i.e., one qubit gets removed
) -> None:
"""
Args:
G (nx.Graph): The input graph on which the Max k-Cut problem is defined.
k_cuts (int): The number of partitions (colors) to cut the graph into.
color_encoding (str): The encoding scheme for colors, e.g., "LessThanK" or "max_balanced".
method (str): The method used for circuit construction, one of "PauliBasis", "PowerOfTwo", or "Diffusion".
fix_one_node (bool): If True, fixes the last node to a specific color, reducing the number of variables.
"""
MaxKCutBinaryFullH.validate_parameters(k_cuts, method, fix_one_node)
self.k_cuts = k_cuts
self.color_encoding = color_encoding
self.method = method
N_qubits_per_node = int(np.ceil(np.log2(self.k_cuts)))
super().__init__(G, N_qubits_per_node, fix_one_node)
if self.method == "PauliBasis":
self.op, self.ophalf = self.getPauliOperator(
self.k_cuts, color_encoding=self.color_encoding
)
elif self.method == "PowerOfTwo":
if self.k_cuts == 3:
k = 4
else:
k = 8
self.mkcb_pot = MaxKCutBinaryPowerOfTwo(G, k, method="Diffusion")
self.N_ancilla_qubits = 2
else: # if self.method == "Diffusion":
pass
self.construct_colors()
@staticmethod
def validate_parameters(k, method, fix_one_node) -> None:
"""
Validates the input parameters for k, method, and fix_one_node.
Args:
k (int): Number of partitions (colors).
method (str): Circuit construction method ("PauliBasis", "PowerOfTwo", or "Diffusion").
fix_one_node (bool): Whether to fix the last node to a specific color.
Raises:
ValueError: If k is not in [3, 5, 6, 7].
ValueError: If method is not valid.
ValueError: If method is "PowerOfTwo" and fix_one_node is True.
"""
### 1) k_cuts needs to be 3, 5, 6, or 7
valid_ks = [3, 5, 6, 7]
if k not in valid_ks:
raise ValueError("k_cuts must be in " + str(valid_ks))
### 2) method
valid_methods = ["PauliBasis", "PowerOfTwo", "Diffusion"]
if method not in valid_methods:
raise ValueError("method must be in " + str(valid_methods))
if method == "PowerOfTwo" and fix_one_node:
raise ValueError(
'For the PowerOfTwo method it "fix_one_node" is not implemented. Use "PauliBasis" or "Diffusion" instead.'
)
def construct_colors(self):
"""
Constructs the mapping from binary strings to color classes based on k and encoding.
Raises:
ValueError: If color_encoding is invalid or unspecified for the given k.
"""
if self.k_cuts == 3:
if self.color_encoding == "LessThanK":
self.colors = {
"color1": ["00"],
"color2": ["01"],
"color3": ["10", "11"],
}
else:
raise ValueError("invalid or unspecified color_encoding")
elif self.k_cuts == 5:
if self.color_encoding == "LessThanK":
self.colors = {
"color1": ["000"],
"color2": ["001"],
"color3": ["010"],
"color4": ["011"],
"color5": ["100", "101", "110", "111"],
}
elif self.color_encoding == "max_balanced":
self.colors = {
"color1": ["000", "001"],
"color2": ["010"],
"color3": ["011"],
"color4": ["100", "101"],
"color5": ["110", "111"],
}
else:
raise ValueError("invalid or unspecified color_encoding")
elif self.k_cuts == 6:
if self.color_encoding == "LessThanK":
self.colors = {
"color1": ["000"],
"color2": ["001"],
"color3": ["010"],
"color4": ["011"],
"color5": ["100"],
"color6": ["101", "110", "111"],
}
elif self.color_encoding in ["max_balanced"]:
self.colors = {
"color1": ["000", "001"],
"color2": ["010"],
"color3": ["011"],
"color4": ["100", "101"],
"color5": ["110"],
"color6": ["111"],
}
else:
raise ValueError("invalid or unspecified color_encoding")
else: # if self.k_cuts == 7:
if self.color_encoding == "LessThanK":
self.colors = {
"color1": ["000"],
"color2": ["001"],
"color3": ["010"],
"color4": ["011"],
"color5": ["100"],
"color6": ["101"],
"color7": ["110", "111"],
}
else:
raise ValueError("invalid or unspecified color_encoding")
# Create a dictionary to map each index to its corresponding set
self.bitstring_to_color = {}
for key, indices in self.colors.items():
for index in indices:
self.bitstring_to_color[index] = key
def apply_N(self, circuit, binary_str1, binary_str2):
"""
Applies X gates to the circuit to map between two binary color encodings.
Args:
circuit (QuantumCircuit): The quantum circuit to modify.
binary_str1 (str): Binary string for the first color encoding.
binary_str2 (str): Binary string for the second color encoding.
Returns:
circuit (QuantumCircuit): The modified quantum circuit.
"""
# Apply X-gates based on the first binary string
for i, bit in enumerate(binary_str1):
if bit == "0":
circuit.x(i)
# Apply X-gates based on the second binary string
for i, bit in enumerate(binary_str2):
if bit == "0":
circuit.x(self.N_qubits_per_node + i)
return circuit
def add_equalize_color(self, qc, bs1, bs2, theta):
"""
Adds gates to the circuit to equalize the phase between two color encodings.
Args:
qc (QuantumCircuit): The quantum circuit to modify.
bs1 (str): Binary string for the first color encoding.
bs2 (str): Binary string for the second color encoding.
theta (float): The phase parameter.
Returns:
qc (QuantumCircuit): The modified quantum circuit.
"""
qc = self.apply_N(qc, bs1, bs2)
qc.barrier()
qc.mcx(
[qc.qubits[i] for i in range(0, self.N_qubits_per_node)],
[qc.qubits[2 * self.N_qubits_per_node]],
)
qc.mcx(
[
qc.qubits[self.N_qubits_per_node + i]
for i in range(0, self.N_qubits_per_node)
],
[qc.qubits[2 * self.N_qubits_per_node + 1]],
)
# C^{n-1}Phase
phase_gate = PhaseGate(-theta).control(2)
qc.append(
phase_gate,
[
2 * self.N_qubits_per_node + 1,
2 * self.N_qubits_per_node,
2 * self.N_qubits_per_node - 1,
],
)
qc.mcx(
[
qc.qubits[self.N_qubits_per_node + i]
for i in range(0, self.N_qubits_per_node)
],
[qc.qubits[2 * self.N_qubits_per_node + 1]],
)
qc.mcx(
[qc.qubits[i] for i in range(0, self.N_qubits_per_node)],
[qc.qubits[2 * self.N_qubits_per_node]],
)
qc.barrier()
qc = self.apply_N(qc, bs1, bs2)
return qc
def create_edge_circuit(self, theta):
"""
Creates the parameterized quantum circuit for a graph edge according to the chosen method and color encoding.
Args:
theta (float): The phase parameter for the circuit.
Returns:
qc (QuantumCircuit): The constructed quantum circuit for the edge.
"""
q = QuantumRegister(2 * self.N_qubits_per_node)
if self.method == "PauliBasis":
qc = QuantumCircuit(q)
qc.append(PauliEvolutionGate(self.op, time=theta), qc.qubits)
elif self.method == "PowerOfTwo":
a = AncillaRegister(self.N_ancilla_qubits)
qc = QuantumCircuit(q, a)
qubits_to_map = list(range(2 * self.N_qubits_per_node))
ancilla_to_map = []
qc.append(
self.mkcb_pot.create_edge_circuit(theta),
q[qubits_to_map] + (a[ancilla_to_map] if ancilla_to_map else []),
)
for _, bitstrings in self.colors.items():
if len(bitstrings) > 1:
pairs = list(itertools.combinations(bitstrings, 2))
for bs1, bs2 in pairs:
qc = self.add_equalize_color(qc, bs1, bs2, theta)
qc = self.add_equalize_color(qc, bs2, bs1, theta)
# target_qubits = [q[i] for i in range(2*self.N_qubits_per_node)] + [
# a[i] for i in range(2)
# ] # Map k qubits and 2 ancillas
# parameterized_circuit.append(small_circuit.to_instruction(), target_qubits)
else: # if self.method == "Diffusion":
qc = QuantumCircuit(q)
if self.k_cuts == 3:
phase_gate = PhaseGate(-theta).control(1)
qc.append(phase_gate, [0, 2])
qc.cx(1, 3)
qc.x([0, 2, 3])
phase_gate = PhaseGate(-theta).control(2)
qc.append(phase_gate, [0, 2, 3])
qc.x([0, 2, 3])
qc.cx(1, 3)
elif self.k_cuts == 5:
if self.color_encoding == "max_balanced":
qc.cx(0, 3)
qc.cx(1, 4)
qc.cx(2, 5)
qc.x([3, 4, 5])
phase_gate = PhaseGate(-theta).control(2)
qc.append(phase_gate, [3, 4, 5])
qc.x([3, 4, 5])
qc.cx(2, 5)
qc.cx(1, 4)
qc.cx(5, 2, ctrl_state=0)
qc.x([1, 2, 3, 4])
phase_gate = PhaseGate(-theta).control(3)
qc.append(phase_gate, [1, 2, 3, 4])
qc.x([1, 2, 3, 4])
qc.cx(5, 2, ctrl_state=0)
qc.cx(0, 3)
qc.cx(2, 5)
phase_gate = PhaseGate(-theta).control(4)
qc.append(phase_gate, [0, 1, 3, 4, 5])
qc.cx(2, 5)
else: # self.color_encoding=="LessThanK":
phase_gate = PhaseGate(-theta).control(1)
qc.append(phase_gate, [0, 3])
qc.cx(1, 4)
qc.cx(2, 5)
qc.x([0, 3, 4, 5])
phase_gate = PhaseGate(-theta).control(3)
qc.append(phase_gate, [0, 3, 4, 5])
qc.x([0, 3, 4, 5])
qc.cx(2, 5)
qc.cx(1, 4)
elif self.k_cuts == 6:
if self.color_encoding == "max_balanced":
qc.cx(0, 3)
qc.cx(1, 4)
qc.cx(2, 5)
qc.x([3, 4, 5])
phase_gate = PhaseGate(-theta).control(2)
qc.append(phase_gate, [3, 4, 5])
qc.x([3, 4, 5])
qc.cx(2, 5)
qc.cx(1, 4)
qc.cx(5, 2, ctrl_state=0)
qc.x([1, 2, 3, 4])
phase_gate = PhaseGate(-theta).control(3)
qc.append(phase_gate, [1, 2, 3, 4])
qc.x([1, 2, 3, 4])
qc.cx(5, 2, ctrl_state=0)
qc.cx(0, 3)
else: # self.color_encoding=="LessThanK":
qc.cx(0, 3)
qc.cx(1, 4)
qc.cx(2, 5)
qc.x([3, 4, 5])
phase_gate = PhaseGate(-theta).control(2)
qc.append(phase_gate, [3, 4, 5])
qc.x([3, 4, 5])
qc.cx(2, 5)
qc.cx(1, 4)
qc.cx(0, 3)
qc.ccx(2, 4, 5)
phase_gate = PhaseGate(-theta).control(3)
qc.append(phase_gate, [0, 1, 3, 5])
qc.ccx(2, 4, 5)
qc.x(1)
phase_gate = PhaseGate(-theta).control(4)
qc.append(phase_gate, [0, 1, 2, 3, 4])
qc.x(1)
elif self.k_cuts == 7:
qc.cx(0, 3)
qc.cx(1, 4)
qc.cx(2, 5)
qc.x([3, 4, 5])
phase_gate = PhaseGate(-theta).control(2)
qc.append(phase_gate, [3, 4, 5])
qc.x(5)
phase_gate = PhaseGate(-theta).control(4)
qc.append(phase_gate, [0, 1, 3, 4, 5])
qc.x([3, 4])
qc.cx(2, 5)
qc.cx(1, 4)
qc.cx(0, 3)
return qc
def create_edge_circuit_fixed_node(self, theta):
"""
Creates the parameterized quantum circuit for an edge when one node is fixed to a specific color.
Args:
theta (float): The phase parameter for the circuit.
Returns:
qc (QuantumCircuit): The constructed quantum circuit for the edge with a fixed node.
"""
if self.method == "PauliBasis":
qc = QuantumCircuit(self.N_qubits_per_node)
qc.append(PauliEvolutionGate(self.ophalf, time=theta), qc.qubits)
else:
qc = self.mkcb_pot.create_edge_circuit_fixed_node(theta)
return qc
def getPauliOperator(self, k_cuts, color_encoding):
"""
Returns the Pauli operators for the cost Hamiltonian for the given k and encoding.
Args:
k_cuts (int): Number of partitions (colors).
color_encoding (str): The encoding scheme for colors.
Raises:
ValueError: If color_encoding is invalid or unspecified for the given k.
Returns:
op (SparsePauliOp): The full Pauli operator for the cost Hamiltonian.
ophalf (SparsePauliOp): The half Pauli operator for the fixed-node case.
"""
# flip Pauli strings, because of qiskit's little endian encoding
if k_cuts == 3:
if color_encoding == "LessThanK":
P = [
[-4 / (2**4), Pauli("IIII"[::-1])],
[-4 / (2**4), Pauli("IIZI"[::-1])],
[+4 / (2**4), Pauli("IZIZ"[::-1])],
[+4 / (2**4), Pauli("IZZZ"[::-1])],
[-4 / (2**4), Pauli("ZIII"[::-1])],
[+12 / (2**4), Pauli("ZIZI"[::-1])],
[+4 / (2**4), Pauli("ZZIZ"[::-1])],
[+4 / (2**4), Pauli("ZZZZ"[::-1])],
]
Phalf = [
[-8 / (2**4), Pauli("II"[::-1])],
[+8 / (2**4), Pauli("ZI"[::-1])],
[+8 / (2**4), Pauli("IZ"[::-1])],
[+8 / (2**4), Pauli("ZZ"[::-1])],
]
else:
raise ValueError("invalid or unspecified color_encoding")
elif k_cuts == 5:
if color_encoding == "max_balanced":
# ((0, 1), (2,), (3,), (4,5), (6,7,)):
P = [
[-36 / (2**6), Pauli("IIIIII"[::-1])],
[4 / (2**6), Pauli("IIIIZI"[::-1])],
[-4 / (2**6), Pauli("IIIZII"[::-1])],
[4 / (2**6), Pauli("IIIZZI"[::-1])],
[4 / (2**6), Pauli("IIZIIZ"[::-1])],
[-4 / (2**6), Pauli("IIZIZZ"[::-1])],
[4 / (2**6), Pauli("IIZZIZ"[::-1])],
[-4 / (2**6), Pauli("IIZZZZ"[::-1])],
[4 / (2**6), Pauli("IZIIII"[::-1])],
[28 / (2**6), Pauli("IZIIZI"[::-1])],
[4 / (2**6), Pauli("IZIZII"[::-1])],
[-4 / (2**6), Pauli("IZIZZI"[::-1])],
[-4 / (2**6), Pauli("IZZIIZ"[::-1])],
[4 / (2**6), Pauli("IZZIZZ"[::-1])],
[-4 / (2**6), Pauli("IZZZIZ"[::-1])],
[4 / (2**6), Pauli("IZZZZZ"[::-1])],
[-4 / (2**6), Pauli("ZIIIII"[::-1])],
[4 / (2**6), Pauli("ZIIIZI"[::-1])],
[28 / (2**6), Pauli("ZIIZII"[::-1])],
[4 / (2**6), Pauli("ZIIZZI"[::-1])],
[4 / (2**6), Pauli("ZIZIIZ"[::-1])],
[-4 / (2**6), Pauli("ZIZIZZ"[::-1])],
[4 / (2**6), Pauli("ZIZZIZ"[::-1])],
[-4 / (2**6), Pauli("ZIZZZZ"[::-1])],
[4 / (2**6), Pauli("ZZIIII"[::-1])],
[-4 / (2**6), Pauli("ZZIIZI"[::-1])],
[4 / (2**6), Pauli("ZZIZII"[::-1])],
[28 / (2**6), Pauli("ZZIZZI"[::-1])],
[-4 / (2**6), Pauli("ZZZIIZ"[::-1])],
[4 / (2**6), Pauli("ZZZIZZ"[::-1])],
[-4 / (2**6), Pauli("ZZZZIZ"[::-1])],
[4 / (2**6), Pauli("ZZZZZZ"[::-1])],
]
Phalf = [
[-32 / (2**6), Pauli("III"[::-1])],
[32 / (2**6), Pauli("IZI"[::-1])],
[32 / (2**6), Pauli("ZII"[::-1])],
[32 / (2**6), Pauli("ZZI"[::-1])],
]
elif color_encoding == "LessThanK":
P = [
[-24 / (2**6), Pauli("IIIIII"[::-1])],
[-24 / (2**6), Pauli("IIIZII"[::-1])],
[+8 / (2**6), Pauli("IIZIIZ"[::-1])],
[+8 / (2**6), Pauli("IIZZIZ"[::-1])],
[+8 / (2**6), Pauli("IZIIZI"[::-1])],
[+8 / (2**6), Pauli("IZIZZI"[::-1])],
[+8 / (2**6), Pauli("IZZIZZ"[::-1])],
[+8 / (2**6), Pauli("IZZZZZ"[::-1])],
[-24 / (2**6), Pauli("ZIIIII"[::-1])],
[+40 / (2**6), Pauli("ZIIZII"[::-1])],
[+8 / (2**6), Pauli("ZIZIIZ"[::-1])],
[+8 / (2**6), Pauli("ZIZZIZ"[::-1])],
[+8 / (2**6), Pauli("ZZIIZI"[::-1])],
[+8 / (2**6), Pauli("ZZIZZI"[::-1])],
[+8 / (2**6), Pauli("ZZZIZZ"[::-1])],
[+8 / (2**6), Pauli("ZZZZZZ"[::-1])],
]
Phalf = [
[-48 / (2**6), Pauli("III"[::-1])],
[+16 / (2**6), Pauli("ZII"[::-1])],
[+16 / (2**6), Pauli("IIZ"[::-1])],
[+16 / (2**6), Pauli("ZIZ"[::-1])],
[+16 / (2**6), Pauli("IZI"[::-1])],
[+16 / (2**6), Pauli("ZZI"[::-1])],
[+16 / (2**6), Pauli("IZZ"[::-1])],
[+16 / (2**6), Pauli("ZZZ"[::-1])],
]
else:
raise ValueError("invalid or unspecified color_encoding")
elif k_cuts == 6:
if color_encoding in ["max_balanced"]:
# ((0,1), (2), (3), (4,5), (6), (7))
P = [
[-40 / (2**6), Pauli("IIIIII"[::-1])],
[8 / (2**6), Pauli("IIIIZI"[::-1])],
[8 / (2**6), Pauli("IIZIIZ"[::-1])],
[-8 / (2**6), Pauli("IIZIZZ"[::-1])],
[8 / (2**6), Pauli("IZIIII"[::-1])],
[24 / (2**6), Pauli("IZIIZI"[::-1])],
[-8 / (2**6), Pauli("IZZIIZ"[::-1])],
[8 / (2**6), Pauli("IZZIZZ"[::-1])],
[24 / (2**6), Pauli("ZIIZII"[::-1])],
[8 / (2**6), Pauli("ZIIZZI"[::-1])],
[8 / (2**6), Pauli("ZIZZIZ"[::-1])],
[-8 / (2**6), Pauli("ZIZZZZ"[::-1])],
[8 / (2**6), Pauli("ZZIZII"[::-1])],
[24 / (2**6), Pauli("ZZIZZI"[::-1])],
[-8 / (2**6), Pauli("ZZZZIZ"[::-1])],
[8 / (2**6), Pauli("ZZZZZZ"[::-1])],
]
Phalf = [
[-32 / (2**6), Pauli("III"[::-1])],
[32 / (2**6), Pauli("IZI"[::-1])],
[32 / (2**6), Pauli("ZII"[::-1])],
[32 / (2**6), Pauli("ZZI"[::-1])],
]
elif color_encoding == "LessThanK":
P = [
[-36 / (2**6), Pauli("IIIIII"[::-1])],
[-4 / (2**6), Pauli("IIIIIZ"[::-1])],
[-4 / (2**6), Pauli("IIIIZI"[::-1])],
[-4 / (2**6), Pauli("IIIIZZ"[::-1])],
[-12 / (2**6), Pauli("IIIZII"[::-1])],
[+4 / (2**6), Pauli("IIIZIZ"[::-1])],
[+4 / (2**6), Pauli("IIIZZI"[::-1])],
[+4 / (2**6), Pauli("IIIZZZ"[::-1])],
[-4 / (2**6), Pauli("IIZIII"[::-1])],
[+12 / (2**6), Pauli("IIZIIZ"[::-1])],
[+4 / (2**6), Pauli("IIZIZI"[::-1])],
[+4 / (2**6), Pauli("IIZIZZ"[::-1])],
[+4 / (2**6), Pauli("IIZZII"[::-1])],
[+4 / (2**6), Pauli("IIZZIZ"[::-1])],
[-4 / (2**6), Pauli("IIZZZI"[::-1])],
[-4 / (2**6), Pauli("IIZZZZ"[::-1])],
[-4 / (2**6), Pauli("IZIIII"[::-1])],
[+4 / (2**6), Pauli("IZIIIZ"[::-1])],
[+12 / (2**6), Pauli("IZIIZI"[::-1])],
[+4 / (2**6), Pauli("IZIIZZ"[::-1])],
[+4 / (2**6), Pauli("IZIZII"[::-1])],
[-4 / (2**6), Pauli("IZIZIZ"[::-1])],
[+4 / (2**6), Pauli("IZIZZI"[::-1])],
[-4 / (2**6), Pauli("IZIZZZ"[::-1])],
[-4 / (2**6), Pauli("IZZIII"[::-1])],
[+4 / (2**6), Pauli("IZZIIZ"[::-1])],
[+4 / (2**6), Pauli("IZZIZI"[::-1])],
[+12 / (2**6), Pauli("IZZIZZ"[::-1])],
[+4 / (2**6), Pauli("IZZZII"[::-1])],
[-4 / (2**6), Pauli("IZZZIZ"[::-1])],
[-4 / (2**6), Pauli("IZZZZI"[::-1])],
[+4 / (2**6), Pauli("IZZZZZ"[::-1])],
[-12 / (2**6), Pauli("ZIIIII"[::-1])],
[+4 / (2**6), Pauli("ZIIIIZ"[::-1])],
[+4 / (2**6), Pauli("ZIIIZI"[::-1])],
[+4 / (2**6), Pauli("ZIIIZZ"[::-1])],
[+28 / (2**6), Pauli("ZIIZII"[::-1])],
[-4 / (2**6), Pauli("ZIIZIZ"[::-1])],
[-4 / (2**6), Pauli("ZIIZZI"[::-1])],
[-4 / (2**6), Pauli("ZIIZZZ"[::-1])],
[+4 / (2**6), Pauli("ZIZIII"[::-1])],
[+4 / (2**6), Pauli("ZIZIIZ"[::-1])],
[-4 / (2**6), Pauli("ZIZIZI"[::-1])],
[-4 / (2**6), Pauli("ZIZIZZ"[::-1])],
[-4 / (2**6), Pauli("ZIZZII"[::-1])],
[+12 / (2**6), Pauli("ZIZZIZ"[::-1])],
[+4 / (2**6), Pauli("ZIZZZI"[::-1])],
[+4 / (2**6), Pauli("ZIZZZZ"[::-1])],
[+4 / (2**6), Pauli("ZZIIII"[::-1])],
[-4 / (2**6), Pauli("ZZIIIZ"[::-1])],
[+4 / (2**6), Pauli("ZZIIZI"[::-1])],
[-4 / (2**6), Pauli("ZZIIZZ"[::-1])],
[-4 / (2**6), Pauli("ZZIZII"[::-1])],
[+4 / (2**6), Pauli("ZZIZIZ"[::-1])],
[+12 / (2**6), Pauli("ZZIZZI"[::-1])],
[+4 / (2**6), Pauli("ZZIZZZ"[::-1])],
[+4 / (2**6), Pauli("ZZZIII"[::-1])],
[-4 / (2**6), Pauli("ZZZIIZ"[::-1])],
[-4 / (2**6), Pauli("ZZZIZI"[::-1])],
[+4 / (2**6), Pauli("ZZZIZZ"[::-1])],
[-4 / (2**6), Pauli("ZZZZII"[::-1])],
[+4 / (2**6), Pauli("ZZZZIZ"[::-1])],
[+4 / (2**6), Pauli("ZZZZZI"[::-1])],
[+12 / (2**6), Pauli("ZZZZZZ"[::-1])],
]
Phalf = [
[-48 / (2**6), Pauli("III"[::-1])],
[+16 / (2**6), Pauli("IIZ"[::-1])],
[+16 / (2**6), Pauli("IZI"[::-1])],
[+16 / (2**6), Pauli("IZZ"[::-1])],
[+16 / (2**6), Pauli("ZII"[::-1])],
[+16 / (2**6), Pauli("ZIZ"[::-1])],
[+16 / (2**6), Pauli("ZZI"[::-1])],
[+16 / (2**6), Pauli("ZZZ"[::-1])],
]
else:
raise ValueError("invalid or unspecified color_encoding")
elif k_cuts == 7:
if color_encoding == "LessThanK":
P = [
[-44 / (2**6), Pauli("IIIIII"[::-1])],
[-4 / (2**6), Pauli("IIIIZI"[::-1])],
[-4 / (2**6), Pauli("IIIZII"[::-1])],
[+4 / (2**6), Pauli("IIIZZI"[::-1])],
[+12 / (2**6), Pauli("IIZIIZ"[::-1])],
[+4 / (2**6), Pauli("IIZIZZ"[::-1])],
[+4 / (2**6), Pauli("IIZZIZ"[::-1])],
[-4 / (2**6), Pauli("IIZZZZ"[::-1])],
[-4 / (2**6), Pauli("IZIIII"[::-1])],
[+20 / (2**6), Pauli("IZIIZI"[::-1])],
[+4 / (2**6), Pauli("IZIZII"[::-1])],
[-4 / (2**6), Pauli("IZIZZI"[::-1])],
[+4 / (2**6), Pauli("IZZIIZ"[::-1])],
[+12 / (2**6), Pauli("IZZIZZ"[::-1])],
[-4 / (2**6), Pauli("IZZZIZ"[::-1])],
[+4 / (2**6), Pauli("IZZZZZ"[::-1])],
[-4 / (2**6), Pauli("ZIIIII"[::-1])],
[+4 / (2**6), Pauli("ZIIIZI"[::-1])],
[+20 / (2**6), Pauli("ZIIZII"[::-1])],
[-4 / (2**6), Pauli("ZIIZZI"[::-1])],
[+4 / (2**6), Pauli("ZIZIIZ"[::-1])],
[-4 / (2**6), Pauli("ZIZIZZ"[::-1])],
[+12 / (2**6), Pauli("ZIZZIZ"[::-1])],
[+4 / (2**6), Pauli("ZIZZZZ"[::-1])],
[+4 / (2**6), Pauli("ZZIIII"[::-1])],
[-4 / (2**6), Pauli("ZZIIZI"[::-1])],
[-4 / (2**6), Pauli("ZZIZII"[::-1])],
[+20 / (2**6), Pauli("ZZIZZI"[::-1])],
[-4 / (2**6), Pauli("ZZZIIZ"[::-1])],
[+4 / (2**6), Pauli("ZZZIZZ"[::-1])],
[+4 / (2**6), Pauli("ZZZZIZ"[::-1])],
[+12 / (2**6), Pauli("ZZZZZZ"[::-1])],
]
Phalf = [
[-48 / (2**6), Pauli("III"[::-1])],
[+16 / (2**6), Pauli("IZI"[::-1])],
[+16 / (2**6), Pauli("ZII"[::-1])],
[+16 / (2**6), Pauli("ZZI"[::-1])],
[+16 / (2**6), Pauli("IIZ"[::-1])],
[+16 / (2**6), Pauli("IZZ"[::-1])],
[+16 / (2**6), Pauli("ZIZ"[::-1])],
[+16 / (2**6), Pauli("ZZZ"[::-1])],
]
else:
raise ValueError("invalid or unspecified color_encoding")
# devide coefficients by 2, since:
# "The evolution gates are related to the Pauli rotation gates by a factor of 2"
op = SparsePauliOp([item[1] for item in P], coeffs=[item[0] / 2 for item in P])
ophalf = SparsePauliOp(
[item[1] for item in Phalf], coeffs=[item[0] / 2 for item in Phalf]
)
return op, ophalf