跳转至

CNFSATOracle

QuICT.algorithm.oracle.CNFSATOracle

CNFSATOracle(simulator=None)

Parameters:

  • simulator

    A QuICT circuit simulator.

Source code in QuICT/algorithm/oracle/cnf_oracle.py
def __init__(self, simulator=None):
    """
    Args:
        simulator: A QuICT circuit simulator.
    """
    self.simulator = simulator
    self.mct_generator = MCTOneAux()

check_solution staticmethod

check_solution(variable_data, variable_number, clause_number, CNF_data)

Check if the CNF is satisfied with a given assignment to the variables.

Parameters:

  • variable_data (List[int]) –

    A list of 0 or 1 indicating False or True assigned to each variable.

  • variable_number (int) –

    Number of variable in the CNF.

  • clause_number (int) –

    Number of clause in the CNF.

  • CNF_data (List[List[int]]) –

    A list representing the CNF formula. Can be generated by read_CNF.

Returns: bool: is True if the CNF is satisfied.

Source code in QuICT/algorithm/oracle/cnf_oracle.py
@staticmethod
def check_solution(variable_data, variable_number, clause_number, CNF_data):
    """ Check if the CNF is satisfied with a given assignment to the variables.

    Args:
        variable_data (List[int]): A list of `0` or `1` indicating `False` or `True` assigned to each variable.
        variable_number (int): Number of variable in the CNF.
        clause_number (int): Number of clause in the CNF.
        CNF_data (List[List[int]]): A list representing the CNF formula. Can be generated by `read_CNF`.
    Returns:
        bool: is `True` if the CNF is satisfied.
    """
    cnf_result = 1
    for i in range(clause_number):
        clause_result = 0
        if CNF_data[i + 1] == []:
            clause_result = 1
        else:
            for j in range(len(CNF_data[i + 1])):
                if CNF_data[i + 1][j] > 0:
                    clause_result = clause_result + variable_data[CNF_data[i + 1][j] - 1]
                else:
                    if CNF_data[i + 1][j] < 0:
                        clause_result = clause_result  + (1 - variable_data[-CNF_data[i + 1][j] - 1])
            if clause_result == 0:
                cnf_result = 0
                break

    if cnf_result == 1:
        return True
    else:
        return False

circuit

circuit(cnf_para: str, ancilla_qubits_num: int = 3, dirty_ancilla: int = 0, output_cgate: bool = True) -> Circuit

Based on the given cnf formula, construct a cnf oracle that will do a bit flip on satisfiable assignments.

Parameters:

  • cnf_para (str | list) –

    The path of a CNF file in dimacs format or a CNF variables: (variable_number, clause_number, CNF_data).

  • ancilla_qubits_num (int, default: 3 ) –

    Number of ancilla qubits in the circuit. Needs to be greater than or equal to 3.

  • dirty_ancilla (int, default: 0 ) –

    0 indicates the ancilla qubits are used as clean qubits. >0 for dirty.

  • output_cgate (bool, default: True ) –

    Whether return a compositegate or a circuit.

Returns: Circuit | CompositeGate: the CNF oracle, a circuit or composite gate.

Source code in QuICT/algorithm/oracle/cnf_oracle.py
 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
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
def circuit(
    self, cnf_para: str, ancilla_qubits_num: int = 3, dirty_ancilla: int = 0, output_cgate: bool = True
) -> Circuit:
    """ Based on the given cnf formula, construct a cnf oracle that will do a bit flip on satisfiable assignments.

    Args:
        cnf_para (str | list): The path of a CNF file in dimacs format or a CNF variables: (variable_number,
            clause_number, CNF_data).
        ancilla_qubits_num (int): Number of ancilla qubits in the circuit. Needs to be greater than or equal to 3.
        dirty_ancilla (int): `0` indicates the ancilla qubits are used as clean qubits. >0 for dirty.
        output_cgate (bool): Whether return a compositegate or a circuit.
    Returns:
        Circuit | CompositeGate: the CNF oracle, a circuit or composite gate.
    """
    # check if Aux > 2
    assert ancilla_qubits_num > 2, "Need at least 3 auxiliary qubit."

    # Step 1: Read CNF File
    if isinstance(cnf_para, str):
        variable_number, clause_number, CNF_data = self.read_CNF(cnf_para)
    else:
        assert len(cnf_para) == 3
        variable_number, clause_number, CNF_data = cnf_para

    # Step 2: Construct Circuit
    self._cgate = CompositeGate()
    p = math.floor((ancilla_qubits_num + 1) / 2)
    depth = math.ceil(math.log(clause_number, p))
    target = variable_number
    if clause_number == 1:
        controls = CNF_data[1]
        controls_abs = []
        controls_X = []
        current_Aux = target + 1
        for i in range(len(controls)):
            if controls[i] < 0:
                controls_abs.append(-controls[i] - 1)
            if controls[i] > 0:
                controls_abs.append(controls[i] - 1)
                controls_X.append(controls[i] - 1)
        for i in range(len(controls_X)):
            X | self._cgate(controls_X[i])
        X | self._cgate(target)
        if controls_abs != []:
            self.mct_generator.execute(len(controls_abs) + 2) | self._cgate(
                controls_abs + [target, current_Aux]
            )

        for i in range(len(controls_X)):
            X | self._cgate(controls_X[i])
    else:
        if dirty_ancilla == 0:
            if clause_number < p + 1:
                controls_abs = []
                for j in range(clause_number):
                    controls_abs.append(variable_number + j + 1)
                    self.clause(
                        CNF_data,
                        variable_number,
                        ancilla_qubits_num,
                        j + 1,
                        j + 1,
                        variable_number + j + 1,
                        depth - 1,
                        depth,
                    )
                if controls_abs != []:
                    self.mct_generator.execute(len(controls_abs) + 2) | self._cgate(
                        controls_abs + [target, target - 1]
                    )
                else:
                    X | self._cgate(target)
                for j in range(clause_number):
                    self.clause(
                        CNF_data,
                        variable_number,
                        ancilla_qubits_num,
                        j + 1,
                        j + 1,
                        variable_number + j + 1,
                        depth - 1,
                        depth,
                    )
            else:
                block_len = math.ceil(clause_number / p)
                block_number = math.ceil(clause_number / block_len)
                controls = []

                for j in range(block_number):
                    self.clause(
                        CNF_data,
                        variable_number,
                        ancilla_qubits_num,
                        j * block_len + 1,
                        np.minimum((j + 1) * block_len, clause_number),
                        variable_number + ancilla_qubits_num - p + 1 + j,
                        depth - 1,
                        depth,
                    )
                    controls.append(
                        variable_number + ancilla_qubits_num - p + 1 + j
                    )

                current_Aux = variable_number + 1
                if controls != []:
                    self.mct_generator.execute(len(controls) + 2) | self._cgate(
                        controls + [target, current_Aux]
                    )

                for j in range(block_number):
                    self.clause(
                        CNF_data,
                        variable_number,
                        ancilla_qubits_num,
                        j * block_len + 1,
                        np.minimum((j + 1) * block_len, clause_number),
                        variable_number + ancilla_qubits_num - p + 1 + j,
                        depth - 1,
                        depth,
                    )
        else:  # dirty_ancilla =1
            if clause_number < p + 1:
                controls_abs = []
                for j in range(clause_number):
                    controls_abs.append(variable_number + j + 1)
                    self.clause(
                        CNF_data,
                        variable_number,
                        ancilla_qubits_num,
                        j + 1,
                        j + 1,
                        variable_number + j + 1,
                        depth - 1,
                        depth,
                    )
                if controls_abs != []:
                    self.mct_generator.execute(len(controls_abs) + 2) | self._cgate(
                        controls_abs + [target, target - 1]
                    )

                for j in range(clause_number):
                    self.clause(
                        CNF_data,
                        variable_number,
                        ancilla_qubits_num,
                        j + 1,
                        j + 1,
                        variable_number + j + 1,
                        depth - 1,
                        depth,
                    )
            else:
                if clause_number == 2:  # to do

                    CCX | self._cgate(
                        [
                            variable_number + ancilla_qubits_num - 1,
                            variable_number + ancilla_qubits_num,
                            target,
                        ]
                    )
                    self.clause(
                        CNF_data,
                        variable_number,
                        ancilla_qubits_num,
                        1,
                        1,
                        variable_number + ancilla_qubits_num - 1,
                        depth - 1,
                        depth,
                    )

                    CCX | self._cgate(
                        [
                            variable_number + ancilla_qubits_num - 1,
                            variable_number + ancilla_qubits_num,
                            target,
                        ]
                    )
                    self.clause(
                        CNF_data,
                        variable_number,
                        ancilla_qubits_num,
                        clause_number,
                        clause_number,
                        variable_number + ancilla_qubits_num,
                        depth - 1,
                        depth,
                    )

                    CCX | self._cgate(
                        [
                            variable_number + ancilla_qubits_num - 1,
                            variable_number + ancilla_qubits_num,
                            target,
                        ]
                    )
                    self.clause(
                        CNF_data,
                        variable_number,
                        ancilla_qubits_num,
                        1,
                        1,
                        variable_number + ancilla_qubits_num - 1,
                        depth - 1,
                        depth,
                    )

                    CCX | self._cgate(
                        [
                            variable_number + ancilla_qubits_num - 1,
                            variable_number + ancilla_qubits_num,
                            target,
                        ]
                    )
                    self.clause(
                        CNF_data,
                        variable_number,
                        ancilla_qubits_num,
                        clause_number,
                        clause_number,
                        variable_number + ancilla_qubits_num,
                        depth - 1,
                        depth,
                    )

                else:
                    block_len = math.ceil(clause_number / p)
                    block_number = math.ceil(clause_number / block_len)
                    current_depth = depth
                    # even
                    if block_number == 2:
                        CCX | self._cgate(
                            [
                                variable_number + ancilla_qubits_num - 1,
                                variable_number + ancilla_qubits_num,
                                target,
                            ]
                        )
                        self.clause(
                            CNF_data,
                            variable_number,
                            ancilla_qubits_num,
                            1,
                            block_len,
                            variable_number + ancilla_qubits_num - 1,
                            depth - 1,
                            depth,
                        )

                        CCX | self._cgate(
                            [
                                variable_number + ancilla_qubits_num - 1,
                                variable_number + ancilla_qubits_num,
                                target,
                            ]
                        )
                        self.clause(
                            CNF_data,
                            variable_number,
                            ancilla_qubits_num,
                            block_len + 1,
                            clause_number,
                            variable_number + ancilla_qubits_num,
                            depth - 1,
                            depth,
                        )

                        CCX | self._cgate(
                            [
                                variable_number + ancilla_qubits_num - 1,
                                variable_number + ancilla_qubits_num,
                                target,
                            ]
                        )
                        self.clause(
                            CNF_data,
                            variable_number,
                            ancilla_qubits_num,
                            1,
                            block_len,
                            variable_number + ancilla_qubits_num - 1,
                            depth - 1,
                            depth,
                        )

                        CCX | self._cgate(
                            [
                                variable_number + ancilla_qubits_num - 1,
                                variable_number + ancilla_qubits_num,
                                target,
                            ]
                        )
                        self.clause(
                            CNF_data,
                            variable_number,
                            ancilla_qubits_num,
                            block_len + 1,
                            clause_number,
                            variable_number + ancilla_qubits_num,
                            depth - 1,
                            depth,
                        )

                    else:  # block_number > 2
                        c = []
                        for i in range(
                            variable_number,
                            variable_number + ancilla_qubits_num + 1,
                        ):
                            if i != target:
                                c.append(i)

                        CCX | self._cgate(
                            [
                                c[ancilla_qubits_num - block_number],
                                c[ancilla_qubits_num - 2 * (block_number - 1)],
                                target,
                            ]
                        )
                        self.clause(
                            CNF_data,
                            variable_number,
                            ancilla_qubits_num,
                            1,
                            block_len,
                            c[ancilla_qubits_num - block_number],
                            current_depth - 1,
                            depth,
                        )
                        CCX | self._cgate(
                            [
                                c[ancilla_qubits_num - block_number],
                                c[ancilla_qubits_num - 2 * (block_number - 1)],
                                target,
                            ]
                        )

                        # Control bit variable_ Number+Aux - (block_number-1)+2 - j
                        # put a clause, another control bit
                        # (will be the same as the previous target in this for)
                        # and the target will rise in turn
                        for j in range(1, block_number - 2):
                            CCX | self._cgate(
                                [
                                    c[
                                        ancilla_qubits_num
                                        - (block_number - 1)
                                        + j
                                        - 1
                                    ],
                                    c[
                                        ancilla_qubits_num
                                        - 2 * (block_number - 1)
                                        + j
                                    ],
                                    c[
                                        ancilla_qubits_num
                                        - 2 * (block_number - 1)
                                        - 1
                                        + j
                                    ],
                                ]
                            )
                            self.clause(
                                CNF_data,
                                variable_number,
                                ancilla_qubits_num,
                                1 + j * block_len,
                                (1 + j) * block_len,
                                c[ancilla_qubits_num - (block_number - 1) + j - 1],
                                current_depth - 1,
                                depth,
                            )
                            CCX | self._cgate(
                                [
                                    c[
                                        ancilla_qubits_num
                                        - (block_number - 1)
                                        + j
                                        - 1
                                    ],
                                    c[
                                        ancilla_qubits_num
                                        - 2 * (block_number - 1)
                                        + j
                                    ],
                                    c[
                                        ancilla_qubits_num
                                        - 2 * (block_number - 1)
                                        - 1
                                        + j
                                    ],
                                ]
                            )

                            # topPhase
                        CCX | self._cgate(
                            [
                                c[ancilla_qubits_num - 2],
                                c[ancilla_qubits_num - 1],
                                c[ancilla_qubits_num - 2 - (block_number - 1)],
                            ]
                        )
                        self.clause(
                            CNF_data,
                            variable_number,
                            ancilla_qubits_num,
                            1 + (block_number - 2) * block_len,
                            (block_number - 1) * block_len,
                            c[ancilla_qubits_num - 2],
                            current_depth - 1,
                            depth,
                        )

                        CCX | self._cgate(
                            [
                                c[ancilla_qubits_num - 2],
                                c[ancilla_qubits_num - 1],
                                c[ancilla_qubits_num - 2 - (block_number - 1)],
                            ]
                        )
                        self.clause(
                            CNF_data,
                            variable_number,
                            ancilla_qubits_num,
                            1 + (block_number - 1) * block_len,
                            clause_number,
                            c[ancilla_qubits_num - 1],
                            current_depth - 1,
                            depth,
                        )

                        CCX | self._cgate(
                            [
                                c[ancilla_qubits_num - 2],
                                c[ancilla_qubits_num - 1],
                                c[ancilla_qubits_num - 2 - (block_number - 1)],
                            ]
                        )
                        self.clause(
                            CNF_data,
                            variable_number,
                            ancilla_qubits_num,
                            1 + (block_number - 2) * block_len,
                            (block_number - 1) * block_len,
                            c[ancilla_qubits_num - 2],
                            current_depth - 1,
                            depth,
                        )

                        CCX | self._cgate(
                            [
                                c[ancilla_qubits_num - 2],
                                c[ancilla_qubits_num - 1],
                                c[ancilla_qubits_num - 2 - (block_number - 1)],
                            ]
                        )

                        for j in range(block_number - 3, 0, -1):
                            CCX | self._cgate(
                                [
                                    c[
                                        ancilla_qubits_num
                                        - (block_number - 1)
                                        + j
                                        - 1
                                    ],
                                    c[
                                        ancilla_qubits_num
                                        - 2 * (block_number - 1)
                                        + j
                                    ],
                                    c[
                                        ancilla_qubits_num
                                        - 2 * (block_number - 1)
                                        - 1
                                        + j
                                    ],
                                ]
                            )
                            self.clause(
                                CNF_data,
                                variable_number,
                                ancilla_qubits_num,
                                1 + j * block_len,
                                (1 + j) * block_len,
                                c[ancilla_qubits_num - (block_number - 1) + j - 1],
                                current_depth - 1,
                                depth,
                            )
                            CCX | self._cgate(
                                [
                                    c[
                                        ancilla_qubits_num
                                        - (block_number - 1)
                                        + j
                                        - 1
                                    ],
                                    c[
                                        ancilla_qubits_num
                                        - 2 * (block_number - 1)
                                        + j
                                    ],
                                    c[
                                        ancilla_qubits_num
                                        - 2 * (block_number - 1)
                                        - 1
                                        + j
                                    ],
                                ]
                            )

                        CCX | self._cgate(
                            [
                                c[ancilla_qubits_num - block_number],
                                c[ancilla_qubits_num - 2 * (block_number - 1)],
                                target,
                            ]
                        )
                        self.clause(
                            CNF_data,
                            variable_number,
                            ancilla_qubits_num,
                            1,
                            block_len,
                            c[ancilla_qubits_num - block_number],
                            current_depth - 1,
                            depth,
                        )
                        CCX | self._cgate(
                            [
                                c[ancilla_qubits_num - block_number],
                                c[ancilla_qubits_num - 2 * (block_number - 1)],
                                target,
                            ]
                        )

                        # repeat
                        for j in range(1, block_number - 2):
                            CCX | self._cgate(
                                [
                                    c[
                                        ancilla_qubits_num
                                        - (block_number - 1)
                                        + j
                                        - 1
                                    ],
                                    c[
                                        ancilla_qubits_num
                                        - 2 * (block_number - 1)
                                        + j
                                    ],
                                    c[
                                        ancilla_qubits_num
                                        - 2 * (block_number - 1)
                                        - 1
                                        + j
                                    ],
                                ]
                            )
                            self.clause(
                                CNF_data,
                                variable_number,
                                ancilla_qubits_num,
                                1 + j * block_len,
                                (1 + j) * block_len,
                                c[ancilla_qubits_num - (block_number - 1) + j - 1],
                                current_depth - 1,
                                depth,
                            )
                            CCX | self._cgate(
                                [
                                    c[
                                        ancilla_qubits_num
                                        - (block_number - 1)
                                        + j
                                        - 1
                                    ],
                                    c[
                                        ancilla_qubits_num
                                        - 2 * (block_number - 1)
                                        + j
                                    ],
                                    c[
                                        ancilla_qubits_num
                                        - 2 * (block_number - 1)
                                        - 1
                                        + j
                                    ],
                                ]
                            )

                            # topPhase
                        CCX | self._cgate(
                            [
                                c[ancilla_qubits_num - 2],
                                c[ancilla_qubits_num - 1],
                                c[ancilla_qubits_num - 2 - (block_number - 1)],
                            ]
                        )
                        self.clause(
                            CNF_data,
                            variable_number,
                            ancilla_qubits_num,
                            1 + (block_number - 2) * block_len,
                            (block_number - 1) * block_len,
                            c[ancilla_qubits_num - 2],
                            current_depth - 1,
                            depth,
                        )

                        CCX | self._cgate(
                            [
                                c[ancilla_qubits_num - 2],
                                c[ancilla_qubits_num - 1],
                                c[ancilla_qubits_num - 2 - (block_number - 1)],
                            ]
                        )
                        self.clause(
                            CNF_data,
                            variable_number,
                            ancilla_qubits_num,
                            1 + (block_number - 1) * block_len,
                            clause_number,
                            c[ancilla_qubits_num - 1],
                            current_depth - 1,
                            depth,
                        )

                        CCX | self._cgate(
                            [
                                c[ancilla_qubits_num - 2],
                                c[ancilla_qubits_num - 1],
                                c[ancilla_qubits_num - 2 - (block_number - 1)],
                            ]
                        )
                        self.clause(
                            CNF_data,
                            variable_number,
                            ancilla_qubits_num,
                            1 + (block_number - 2) * block_len,
                            (block_number - 1) * block_len,
                            c[ancilla_qubits_num - 2],
                            current_depth - 1,
                            depth,
                        )

                        CCX | self._cgate(
                            [
                                c[ancilla_qubits_num - 2],
                                c[ancilla_qubits_num - 1],
                                c[ancilla_qubits_num - 2 - (block_number - 1)],
                            ]
                        )

                        for j in range(block_number - 3, 0, -1):
                            CCX | self._cgate(
                                [
                                    c[
                                        ancilla_qubits_num
                                        - (block_number - 1)
                                        + j
                                        - 1
                                    ],
                                    c[
                                        ancilla_qubits_num
                                        - 2 * (block_number - 1)
                                        + j
                                    ],
                                    c[
                                        ancilla_qubits_num
                                        - 2 * (block_number - 1)
                                        - 1
                                        + j
                                    ],
                                ]
                            )
                            self.clause(
                                CNF_data,
                                variable_number,
                                ancilla_qubits_num,
                                1 + j * block_len,
                                (1 + j) * block_len,
                                c[ancilla_qubits_num - (block_number - 1) + j - 1],
                                current_depth - 1,
                                depth,
                            )
                            CCX | self._cgate(
                                [
                                    c[
                                        ancilla_qubits_num
                                        - (block_number - 1)
                                        + j
                                        - 1
                                    ],
                                    c[
                                        ancilla_qubits_num
                                        - 2 * (block_number - 1)
                                        + j
                                    ],
                                    c[
                                        ancilla_qubits_num
                                        - 2 * (block_number - 1)
                                        - 1
                                        + j
                                    ],
                                ]
                            )

    if output_cgate:
        return self._cgate
    else:
        cnf_circuit = Circuit(self._cgate.width())
        self._cgate | cnf_circuit
        return cnf_circuit

find_solution_count classmethod

find_solution_count(variable_number, clause_number, CNF_data)

Use classical brute force method to find the number of solutions that make the CNF satisfied.

Parameters:

  • variable_number (int) –

    Number of variable in the CNF.

  • clause_number (int) –

    Number of clause in the CNF.

  • CNF_data (List[List[int]]) –

    A list representing the CNF formula. Can be generated by read_CNF.

Returns: int: number of the satisfiable solutions.

Source code in QuICT/algorithm/oracle/cnf_oracle.py
@classmethod
def find_solution_count(cls, variable_number, clause_number, CNF_data):
    """ Use classical brute force method to find the number of solutions that make the CNF satisfied.

    Args:
        variable_number (int): Number of variable in the CNF.
        clause_number (int): Number of clause in the CNF.
        CNF_data (List[List[int]]): A list representing the CNF formula. Can be generated by `read_CNF`.
    Returns:
        int: number of the satisfiable solutions.
    """
    solutions = []
    for i in range(1 << variable_number):
        variable_data = bin(i)[2:].rjust(variable_number, '0')[::-1]
        variable_data = [int(x) for x in variable_data]
        if cls.check_solution(variable_data, variable_number, clause_number, CNF_data):
            solutions.append(variable_data)

    return len(solutions)

read_CNF staticmethod

read_CNF(cnf_file)

Parse the input CNF file.

Parameters:

  • cnf_file (str) –

    The path of the CNF file in dimacs format.

Returns: (int, int, List[List[int]]): a list containing variable number, clause number and a list representing the CNF formula.

Source code in QuICT/algorithm/oracle/cnf_oracle.py
@staticmethod
def read_CNF(cnf_file):
    """ Parse the input CNF file.

    Args:
        cnf_file (str): The path of the CNF file in dimacs format.
    Returns:
        (int, int, List[List[int]]): a list containing variable number, clause number and a list representing
        the CNF formula.
    """
    # file analysis
    variable_number = 0
    clause_number = 0
    CNF_data = []
    f = open(cnf_file, "r")
    for line in f.readlines():
        new = line.strip().split()
        if new[0] == "c":
            continue
        int_new = []
        if new[0] == "p":
            variable_number = int(new[2])
            clause_number = int(new[3])
        else:
            for x in new:
                if (x != "0") and (int(x) not in int_new):
                    int_new.append(int(x))
                    if (-int(x)) in int_new:
                        int_new = []
                        break

        CNF_data.append(int_new)

    f.close()
    return variable_number, clause_number, CNF_data

run

run(cnf_para: str, ancilla_qubits_num: int = 3, dirty_ancilla: int = 0, shots: int = 0) -> int

Based on the given CNF formula, construct the phase oracle and get sampling result or statevector by running the oracle as a circuit.

Parameters:

  • cnf_para (str | list) –

    The path of a CNF file in dimacs format or a CNF variables: (variable_number, clause_number, CNF_data).

  • ancilla_qubits_num (int, default: 3 ) –

    Number of ancilla qubits in the circuit. Needs to be greater than or equal to 3.

  • dirty_ancilla (int, default: 0 ) –

    0 indicates the ancilla qubits are used as clean qubits. >0 for dirty.

  • shots (int, default: 0 ) –

    Number of shots used in the sampling process. When set to 0, the state vector instead of the sampling result will be returned.

Returns: ndarray | List[int]: when shots<=0, the state-vector will be returned as an ndarray, otherwise the sampling result will be returned.

Source code in QuICT/algorithm/oracle/cnf_oracle.py
def run(
    self, cnf_para: str, ancilla_qubits_num: int = 3, dirty_ancilla: int = 0, shots: int = 0
) -> int:
    """ Based on the given CNF formula, construct the phase oracle and get sampling result or statevector
    by running the oracle as a circuit.

    Args:
        cnf_para (str | list): The path of a CNF file in dimacs format or a CNF variables: (variable_number,
            clause_number, CNF_data).
        ancilla_qubits_num (int): Number of ancilla qubits in the circuit. Needs to be greater than or equal to 3.
        dirty_ancilla (int): `0` indicates the ancilla qubits are used as clean qubits. >0 for dirty.
        shots (int): Number of shots used in the sampling process. When set to `0`, the state vector instead of
            the sampling result will be returned.
    Returns:
        ndarray | List[int]: when `shots<=0`, the state-vector will be returned as an ndarray, otherwise the
            sampling result will be returned.
    """
    # Step 1: Get Circuit
    cnf_circuit = self.circuit(cnf_para, ancilla_qubits_num, dirty_ancilla, output_cgate=False)

    # Step 2: Simulator
    cnf_sv = self.simulator.run(cnf_circuit)

    # Step 3: Sample
    if shots > 0:
        cnf_sample = self.simulator.sample(shots)

        return cnf_sample
    else:
        return cnf_sv