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
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
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
|
%global _empty_manifest_terminate_build 0
Name: python-py-aiger-bv
Version: 4.7.6
Release: 1
Summary: A python library for manipulating sequential and-inverter gates.
License: MIT
URL: https://pypi.org/project/py-aiger-bv/
Source0: https://mirrors.nju.edu.cn/pypi/web/packages/c7/3c/3aa329f97ad2e8b2e020eeefca22189931e8d1fba4f59387b9b1aff389db/py_aiger_bv-4.7.6.tar.gz
BuildArch: noarch
Requires: python3-attrs
Requires: python3-funcy
Requires: python3-py-aiger
Requires: python3-pyrsistent
%description
<figure>
<img src="logo_text.svg" alt="py-aiger-bv logo" width=300px>
<figcaption>pyAiger-BV: Extension of pyAiger for manipulating
sequential bitvector circuits.</figcaption>
</figure>
[](https://cloud.drone.io/mvcisback/py-aiger-bv)
[](https://mvcisback.github.io/py-aiger-bv)
[](https://badge.fury.io/py/py-aiger-bv)
[](https://opensource.org/licenses/MIT)
# Table of Contents
- [About](#about-py-aiger-bv)
- [Installation](#installation)
- [BitVector Expr DSL](#bitvector-expression-dsl)
- [Sequential Circuit DSL](#sequential-circuit-dsl)
# About Py-Aiger-BV
This library provides word level abstractions on top of
[py-aiger](https://github.com/mvcisback/py-aiger). This is done by the
`AIGBV` which groups inputs, outputs, and latches into named
**ordered** sequences, e.g. bitvectors.
The resulting objects can be turned into `AIG`s where each input,
output, or latches name has its index appended to its name. For example,
an bitvector input, `'x'` with 3 bits becomes inputs `'x[0]', 'x[1]', 'x[3]'`
# Installation
If you just need to use `aiger_bv`, you can just run:
`$ pip install py-aiger-bv`
For developers, note that this project uses the
[poetry](https://poetry.eustace.io/) python package/dependency
management tool. Please familarize yourself with it and then
run:
`$ poetry install`
# BitVector Expression DSL
As in py-aiger, when writing combinatorial circuits, the Sequential
Circuit DSL can be somewhat clumsy. For this common usecase, we have
developed the BitVector Expression DSL. This DSL actually consists of
two DSLs for signed and unsigned BitVectors. All circuits generated
this way have a single output word. We use a **big-endian** encoding
where the most significant digit is the first element of the tuple
representing the word. For signed numbers, two's complement is used.
```python
import aiger_bv
# Create 16 bit variables.
x = aiger_bv.atom(16, 'x')
y = aiger_bv.atom(16, 'y', signed=True) # Signed by default.
z = aiger_bv.uatom(16, 'z') # Equiv to signed=False.
# bitwise ops.
expr1 = x & y # Bitwise and.
expr2 = x | y # Bitwise or.
expr3 = x ^ y # Bitwise xor.
expr4 = ~x # Bitwise negation.
# arithmetic
expr5 = x + y
expr6 = x - y
expr7 = x << y
expr8 = x >> y # logical if unsigned, arithmetic if signed.
expr9 = -x # Arithmetic negation. Only defined for signed expr.
expr10 = abs(x)
expr11 = x @ y # inner product of bitvectors mod 2 (+ is xor).
# comparison
expr12 = x == y
expr13 = x != y
expr14 = x < y
expr15 = x <= y
expr16 = x > y
expr17 = x >= y
# Atoms can be constants.
expr18 = x & aiger_bv.atom(16, 3)
expr19 = x & aiger_bv.atom(16, 0xff)
# BitVector expressions can be concatenated.
expr20 = x.concat(y)
# Particular bits can be indexed to create new expressions.
expr21 = x[1]
# Single bit expressions can be repeated.
expr22 = x[1].repeat(10)
# And you can inspect the AIGBV if needed.
circ = x.aigbv
# And you can inspect the AIG if needed.
circ = x.aigbv.aig
# And of course, you can get a BoolExpr from a single output aig.
expr = aiger_bv.UnsignedBVExpr(circ)
```
# Sequential Circuit DSL
py-aiger-bv's Sequential Circuit DSL implements the same basic api as
py-aiger's Sequential Circuit DSL, but operates at the (variable
length) word level rather than the bit level.
```python
import aiger
import aiger_bv
circ = ... # Create a circuit (see below).
# We assume this circuit has word level
# inputs: x,y, outputs: z, w, q, latches: a, b
assert circ.inputs == {'x', 'y'}
assert circ.outputs == {'z', 'w', 'q'}
assert circ.latches == {'a', 'b'}
```
## Sequential composition
```python
circ3 = circ1 >> circ2
```
## Parallel composition
```python
circ3 = circ1 | circ2
```
## Adding Feedback (inserts a delay)
```python
# Connect output y to input x with delay (initialized to True).
# (Default initialization is False.)
cir2 = circ.feedback(
inputs=['x'],
outputs=['y'],
initials=[True],
keep_outputs=True
)
```
## Relabeling
```python
# Relabel input 'x' to 'z'.
circ2 = circ['i', {'x': 'z'}]
# Relabel output 'y' to 'w'.
circ2 = circ['o', {'y': 'w'}]
# Relabel latches 'l1' to 'l2'.
circ2 = circ['l', {'l1': 'l2'}]
```
## Evaluation
```python
# Combinatoric evaluation.
circ(inputs={'x':(True, False, True), 'y': (True, False)})
# Sequential evaluation.
circ.simulate([
{'x': (True, False, True), 'y': (True, False)},
{'x': (False, False, True), 'y': (False, False)},
])
# Simulation Coroutine.
sim = circ.simulator() # Coroutine
next(sim) # Initialize
print(sim.send({'x': (True, False, True), 'y': (True, False)}))
print(sim.send({'x': (False, False, True), 'y': (False, False)}))
# Unroll
circ2 = circ.unroll(steps=10, init=True)
```
## aiger.AIG to aiger.AIGBV
There are two main ways to take an object `AIG` from `aiger` and
convert it into an `AIGBV` object. The first is the `aig2aigbv`
command which simply makes all inputs words of size 1.
```python
# Create aiger_bv.AIGERBV object from aiger.AIG object.
circ = ... # Some aiger.AIG object
word_circ = aiger_bv.aig2aigbv(circ) # aiger_bv.AIGBV object
```
## Gadget Library
### General Manipulation
```python
# Copy outputs 'x' and 'y' to 'w1, w2' and 'z1, z2'.
circ1 = circ >> aiger_bv.tee(wordlen=3, iomap={
'x': ('w1', 'w2'),
'y': ('z1', 'z2')
})
# Take 1 bit output, 'x', duplicate it 5 times, and group into
# a single 5-length word output, 'y'.
circ2 = circ >> aiger_bv.repeat(wordlen=5, input='x', output='z')
# Reverse order of a word.
circ3 = circ >> aiger_bv.reverse_gate(wordlen=5, input='x', output='z')
# Sink and Source circuits (see encoding section for encoding details).
## Always output binary encoding for 15.
circ4 = aiger_bv.source(wordlen=4, value=15, name='x', signed=False)
## Absorb output 'y'
circ5 = circ >> aiger_bv.sink(wordlen=4, inputs=['y'])
# Identity Gate
circ6 = circ >> aiger_bv.identity_gate(wordlen=3, input='x')
# Combine/Concatenate words
circ7 = circ >> aiger_bv.combine_gate(
left_wordlen=3, left='x',
right_wordlen=3, right='y',
output='z'
)
# Split words
circ8 = circ >> aiger_bv.split_gate(
input='x',
left_wordlen=1, left='z',
right_wordlen=2, right='w'
)
# Select single index of circuit and make it a wordlen=1 output.
circ9 = circ >> aiger_bv.index_gate(wordlen=3, idx=1, input='x', output='x1')
```
## Bitwise Operations
- `aiger_bv.bitwise_and(3, left='x', right='y', output='x&y')`
- `aiger_bv.bitwise_or(3, left='x', right='y', output='x|y')`
- `aiger_bv.bitwise_xor(3, left='x', right='y', output='x^y')`
- `aiger_bv.bitwise_negate(3, left='x', output='~x')`
## Arithmetic
- `aiger_bv.add_gate(3, left='x', right='y', output='x+y')`
- `aiger_bv.subtract_gate_gate(3, left='x', right='y', output='x-y')`
- `aiger_bv.inc_gate(3, left='x', output='x+1')`
- `aiger_bv.dec_gate(3, left='x', output='x+1')`
- `aiger_bv.negate_gate(3, left='x', output='-x')`
- `aiger_bv.logical_right_shift(3, shift=1, input='x', output='x>>1')`
- `aiger_bv.arithmetic_right_shift(3, shift=1, input='x', output='x>>1')`
- `aiger_bv.left_shift(3, shift=1, input='x', output='x<<1')`
## Comparison
- `aiger_bv.is_nonzero_gate(3, input='x', output='is_nonzero')`
- `aiger_bv.is_zero_gate(3, input='x', output='is_zero')`
- `aiger_bv.eq_gate(3, left='x', right='y', output='x=y')`
- `aiger_bv.ne_gate(3, left='x', right='y', output='x!=y')`
- `aiger_bv.unsigned_lt_gate(3, left='x', right='y', output='x<y')`
- `aiger_bv.unsigned_gt_gate(3, left='x', right='y', output='x>y')`
- `aiger_bv.unsigned_le_gate(3, left='x', right='y', output='x<=y')`
- `aiger_bv.unsigned_ge_gate(3, left='x', right='y', output='x>=y')`
- `aiger_bv.signed_lt_gate(3, left='x', right='y', output='x<y')`
- `aiger_bv.signed_gt_gate(3, left='x', right='y', output='x>y')`
- `aiger_bv.signed_le_gate(3, left='x', right='y', output='x<=y')`
- `aiger_bv.signed_ge_gate(3, left='x', right='y', output='x>=y')`
%package -n python3-py-aiger-bv
Summary: A python library for manipulating sequential and-inverter gates.
Provides: python-py-aiger-bv
BuildRequires: python3-devel
BuildRequires: python3-setuptools
BuildRequires: python3-pip
%description -n python3-py-aiger-bv
<figure>
<img src="logo_text.svg" alt="py-aiger-bv logo" width=300px>
<figcaption>pyAiger-BV: Extension of pyAiger for manipulating
sequential bitvector circuits.</figcaption>
</figure>
[](https://cloud.drone.io/mvcisback/py-aiger-bv)
[](https://mvcisback.github.io/py-aiger-bv)
[](https://badge.fury.io/py/py-aiger-bv)
[](https://opensource.org/licenses/MIT)
# Table of Contents
- [About](#about-py-aiger-bv)
- [Installation](#installation)
- [BitVector Expr DSL](#bitvector-expression-dsl)
- [Sequential Circuit DSL](#sequential-circuit-dsl)
# About Py-Aiger-BV
This library provides word level abstractions on top of
[py-aiger](https://github.com/mvcisback/py-aiger). This is done by the
`AIGBV` which groups inputs, outputs, and latches into named
**ordered** sequences, e.g. bitvectors.
The resulting objects can be turned into `AIG`s where each input,
output, or latches name has its index appended to its name. For example,
an bitvector input, `'x'` with 3 bits becomes inputs `'x[0]', 'x[1]', 'x[3]'`
# Installation
If you just need to use `aiger_bv`, you can just run:
`$ pip install py-aiger-bv`
For developers, note that this project uses the
[poetry](https://poetry.eustace.io/) python package/dependency
management tool. Please familarize yourself with it and then
run:
`$ poetry install`
# BitVector Expression DSL
As in py-aiger, when writing combinatorial circuits, the Sequential
Circuit DSL can be somewhat clumsy. For this common usecase, we have
developed the BitVector Expression DSL. This DSL actually consists of
two DSLs for signed and unsigned BitVectors. All circuits generated
this way have a single output word. We use a **big-endian** encoding
where the most significant digit is the first element of the tuple
representing the word. For signed numbers, two's complement is used.
```python
import aiger_bv
# Create 16 bit variables.
x = aiger_bv.atom(16, 'x')
y = aiger_bv.atom(16, 'y', signed=True) # Signed by default.
z = aiger_bv.uatom(16, 'z') # Equiv to signed=False.
# bitwise ops.
expr1 = x & y # Bitwise and.
expr2 = x | y # Bitwise or.
expr3 = x ^ y # Bitwise xor.
expr4 = ~x # Bitwise negation.
# arithmetic
expr5 = x + y
expr6 = x - y
expr7 = x << y
expr8 = x >> y # logical if unsigned, arithmetic if signed.
expr9 = -x # Arithmetic negation. Only defined for signed expr.
expr10 = abs(x)
expr11 = x @ y # inner product of bitvectors mod 2 (+ is xor).
# comparison
expr12 = x == y
expr13 = x != y
expr14 = x < y
expr15 = x <= y
expr16 = x > y
expr17 = x >= y
# Atoms can be constants.
expr18 = x & aiger_bv.atom(16, 3)
expr19 = x & aiger_bv.atom(16, 0xff)
# BitVector expressions can be concatenated.
expr20 = x.concat(y)
# Particular bits can be indexed to create new expressions.
expr21 = x[1]
# Single bit expressions can be repeated.
expr22 = x[1].repeat(10)
# And you can inspect the AIGBV if needed.
circ = x.aigbv
# And you can inspect the AIG if needed.
circ = x.aigbv.aig
# And of course, you can get a BoolExpr from a single output aig.
expr = aiger_bv.UnsignedBVExpr(circ)
```
# Sequential Circuit DSL
py-aiger-bv's Sequential Circuit DSL implements the same basic api as
py-aiger's Sequential Circuit DSL, but operates at the (variable
length) word level rather than the bit level.
```python
import aiger
import aiger_bv
circ = ... # Create a circuit (see below).
# We assume this circuit has word level
# inputs: x,y, outputs: z, w, q, latches: a, b
assert circ.inputs == {'x', 'y'}
assert circ.outputs == {'z', 'w', 'q'}
assert circ.latches == {'a', 'b'}
```
## Sequential composition
```python
circ3 = circ1 >> circ2
```
## Parallel composition
```python
circ3 = circ1 | circ2
```
## Adding Feedback (inserts a delay)
```python
# Connect output y to input x with delay (initialized to True).
# (Default initialization is False.)
cir2 = circ.feedback(
inputs=['x'],
outputs=['y'],
initials=[True],
keep_outputs=True
)
```
## Relabeling
```python
# Relabel input 'x' to 'z'.
circ2 = circ['i', {'x': 'z'}]
# Relabel output 'y' to 'w'.
circ2 = circ['o', {'y': 'w'}]
# Relabel latches 'l1' to 'l2'.
circ2 = circ['l', {'l1': 'l2'}]
```
## Evaluation
```python
# Combinatoric evaluation.
circ(inputs={'x':(True, False, True), 'y': (True, False)})
# Sequential evaluation.
circ.simulate([
{'x': (True, False, True), 'y': (True, False)},
{'x': (False, False, True), 'y': (False, False)},
])
# Simulation Coroutine.
sim = circ.simulator() # Coroutine
next(sim) # Initialize
print(sim.send({'x': (True, False, True), 'y': (True, False)}))
print(sim.send({'x': (False, False, True), 'y': (False, False)}))
# Unroll
circ2 = circ.unroll(steps=10, init=True)
```
## aiger.AIG to aiger.AIGBV
There are two main ways to take an object `AIG` from `aiger` and
convert it into an `AIGBV` object. The first is the `aig2aigbv`
command which simply makes all inputs words of size 1.
```python
# Create aiger_bv.AIGERBV object from aiger.AIG object.
circ = ... # Some aiger.AIG object
word_circ = aiger_bv.aig2aigbv(circ) # aiger_bv.AIGBV object
```
## Gadget Library
### General Manipulation
```python
# Copy outputs 'x' and 'y' to 'w1, w2' and 'z1, z2'.
circ1 = circ >> aiger_bv.tee(wordlen=3, iomap={
'x': ('w1', 'w2'),
'y': ('z1', 'z2')
})
# Take 1 bit output, 'x', duplicate it 5 times, and group into
# a single 5-length word output, 'y'.
circ2 = circ >> aiger_bv.repeat(wordlen=5, input='x', output='z')
# Reverse order of a word.
circ3 = circ >> aiger_bv.reverse_gate(wordlen=5, input='x', output='z')
# Sink and Source circuits (see encoding section for encoding details).
## Always output binary encoding for 15.
circ4 = aiger_bv.source(wordlen=4, value=15, name='x', signed=False)
## Absorb output 'y'
circ5 = circ >> aiger_bv.sink(wordlen=4, inputs=['y'])
# Identity Gate
circ6 = circ >> aiger_bv.identity_gate(wordlen=3, input='x')
# Combine/Concatenate words
circ7 = circ >> aiger_bv.combine_gate(
left_wordlen=3, left='x',
right_wordlen=3, right='y',
output='z'
)
# Split words
circ8 = circ >> aiger_bv.split_gate(
input='x',
left_wordlen=1, left='z',
right_wordlen=2, right='w'
)
# Select single index of circuit and make it a wordlen=1 output.
circ9 = circ >> aiger_bv.index_gate(wordlen=3, idx=1, input='x', output='x1')
```
## Bitwise Operations
- `aiger_bv.bitwise_and(3, left='x', right='y', output='x&y')`
- `aiger_bv.bitwise_or(3, left='x', right='y', output='x|y')`
- `aiger_bv.bitwise_xor(3, left='x', right='y', output='x^y')`
- `aiger_bv.bitwise_negate(3, left='x', output='~x')`
## Arithmetic
- `aiger_bv.add_gate(3, left='x', right='y', output='x+y')`
- `aiger_bv.subtract_gate_gate(3, left='x', right='y', output='x-y')`
- `aiger_bv.inc_gate(3, left='x', output='x+1')`
- `aiger_bv.dec_gate(3, left='x', output='x+1')`
- `aiger_bv.negate_gate(3, left='x', output='-x')`
- `aiger_bv.logical_right_shift(3, shift=1, input='x', output='x>>1')`
- `aiger_bv.arithmetic_right_shift(3, shift=1, input='x', output='x>>1')`
- `aiger_bv.left_shift(3, shift=1, input='x', output='x<<1')`
## Comparison
- `aiger_bv.is_nonzero_gate(3, input='x', output='is_nonzero')`
- `aiger_bv.is_zero_gate(3, input='x', output='is_zero')`
- `aiger_bv.eq_gate(3, left='x', right='y', output='x=y')`
- `aiger_bv.ne_gate(3, left='x', right='y', output='x!=y')`
- `aiger_bv.unsigned_lt_gate(3, left='x', right='y', output='x<y')`
- `aiger_bv.unsigned_gt_gate(3, left='x', right='y', output='x>y')`
- `aiger_bv.unsigned_le_gate(3, left='x', right='y', output='x<=y')`
- `aiger_bv.unsigned_ge_gate(3, left='x', right='y', output='x>=y')`
- `aiger_bv.signed_lt_gate(3, left='x', right='y', output='x<y')`
- `aiger_bv.signed_gt_gate(3, left='x', right='y', output='x>y')`
- `aiger_bv.signed_le_gate(3, left='x', right='y', output='x<=y')`
- `aiger_bv.signed_ge_gate(3, left='x', right='y', output='x>=y')`
%package help
Summary: Development documents and examples for py-aiger-bv
Provides: python3-py-aiger-bv-doc
%description help
<figure>
<img src="logo_text.svg" alt="py-aiger-bv logo" width=300px>
<figcaption>pyAiger-BV: Extension of pyAiger for manipulating
sequential bitvector circuits.</figcaption>
</figure>
[](https://cloud.drone.io/mvcisback/py-aiger-bv)
[](https://mvcisback.github.io/py-aiger-bv)
[](https://badge.fury.io/py/py-aiger-bv)
[](https://opensource.org/licenses/MIT)
# Table of Contents
- [About](#about-py-aiger-bv)
- [Installation](#installation)
- [BitVector Expr DSL](#bitvector-expression-dsl)
- [Sequential Circuit DSL](#sequential-circuit-dsl)
# About Py-Aiger-BV
This library provides word level abstractions on top of
[py-aiger](https://github.com/mvcisback/py-aiger). This is done by the
`AIGBV` which groups inputs, outputs, and latches into named
**ordered** sequences, e.g. bitvectors.
The resulting objects can be turned into `AIG`s where each input,
output, or latches name has its index appended to its name. For example,
an bitvector input, `'x'` with 3 bits becomes inputs `'x[0]', 'x[1]', 'x[3]'`
# Installation
If you just need to use `aiger_bv`, you can just run:
`$ pip install py-aiger-bv`
For developers, note that this project uses the
[poetry](https://poetry.eustace.io/) python package/dependency
management tool. Please familarize yourself with it and then
run:
`$ poetry install`
# BitVector Expression DSL
As in py-aiger, when writing combinatorial circuits, the Sequential
Circuit DSL can be somewhat clumsy. For this common usecase, we have
developed the BitVector Expression DSL. This DSL actually consists of
two DSLs for signed and unsigned BitVectors. All circuits generated
this way have a single output word. We use a **big-endian** encoding
where the most significant digit is the first element of the tuple
representing the word. For signed numbers, two's complement is used.
```python
import aiger_bv
# Create 16 bit variables.
x = aiger_bv.atom(16, 'x')
y = aiger_bv.atom(16, 'y', signed=True) # Signed by default.
z = aiger_bv.uatom(16, 'z') # Equiv to signed=False.
# bitwise ops.
expr1 = x & y # Bitwise and.
expr2 = x | y # Bitwise or.
expr3 = x ^ y # Bitwise xor.
expr4 = ~x # Bitwise negation.
# arithmetic
expr5 = x + y
expr6 = x - y
expr7 = x << y
expr8 = x >> y # logical if unsigned, arithmetic if signed.
expr9 = -x # Arithmetic negation. Only defined for signed expr.
expr10 = abs(x)
expr11 = x @ y # inner product of bitvectors mod 2 (+ is xor).
# comparison
expr12 = x == y
expr13 = x != y
expr14 = x < y
expr15 = x <= y
expr16 = x > y
expr17 = x >= y
# Atoms can be constants.
expr18 = x & aiger_bv.atom(16, 3)
expr19 = x & aiger_bv.atom(16, 0xff)
# BitVector expressions can be concatenated.
expr20 = x.concat(y)
# Particular bits can be indexed to create new expressions.
expr21 = x[1]
# Single bit expressions can be repeated.
expr22 = x[1].repeat(10)
# And you can inspect the AIGBV if needed.
circ = x.aigbv
# And you can inspect the AIG if needed.
circ = x.aigbv.aig
# And of course, you can get a BoolExpr from a single output aig.
expr = aiger_bv.UnsignedBVExpr(circ)
```
# Sequential Circuit DSL
py-aiger-bv's Sequential Circuit DSL implements the same basic api as
py-aiger's Sequential Circuit DSL, but operates at the (variable
length) word level rather than the bit level.
```python
import aiger
import aiger_bv
circ = ... # Create a circuit (see below).
# We assume this circuit has word level
# inputs: x,y, outputs: z, w, q, latches: a, b
assert circ.inputs == {'x', 'y'}
assert circ.outputs == {'z', 'w', 'q'}
assert circ.latches == {'a', 'b'}
```
## Sequential composition
```python
circ3 = circ1 >> circ2
```
## Parallel composition
```python
circ3 = circ1 | circ2
```
## Adding Feedback (inserts a delay)
```python
# Connect output y to input x with delay (initialized to True).
# (Default initialization is False.)
cir2 = circ.feedback(
inputs=['x'],
outputs=['y'],
initials=[True],
keep_outputs=True
)
```
## Relabeling
```python
# Relabel input 'x' to 'z'.
circ2 = circ['i', {'x': 'z'}]
# Relabel output 'y' to 'w'.
circ2 = circ['o', {'y': 'w'}]
# Relabel latches 'l1' to 'l2'.
circ2 = circ['l', {'l1': 'l2'}]
```
## Evaluation
```python
# Combinatoric evaluation.
circ(inputs={'x':(True, False, True), 'y': (True, False)})
# Sequential evaluation.
circ.simulate([
{'x': (True, False, True), 'y': (True, False)},
{'x': (False, False, True), 'y': (False, False)},
])
# Simulation Coroutine.
sim = circ.simulator() # Coroutine
next(sim) # Initialize
print(sim.send({'x': (True, False, True), 'y': (True, False)}))
print(sim.send({'x': (False, False, True), 'y': (False, False)}))
# Unroll
circ2 = circ.unroll(steps=10, init=True)
```
## aiger.AIG to aiger.AIGBV
There are two main ways to take an object `AIG` from `aiger` and
convert it into an `AIGBV` object. The first is the `aig2aigbv`
command which simply makes all inputs words of size 1.
```python
# Create aiger_bv.AIGERBV object from aiger.AIG object.
circ = ... # Some aiger.AIG object
word_circ = aiger_bv.aig2aigbv(circ) # aiger_bv.AIGBV object
```
## Gadget Library
### General Manipulation
```python
# Copy outputs 'x' and 'y' to 'w1, w2' and 'z1, z2'.
circ1 = circ >> aiger_bv.tee(wordlen=3, iomap={
'x': ('w1', 'w2'),
'y': ('z1', 'z2')
})
# Take 1 bit output, 'x', duplicate it 5 times, and group into
# a single 5-length word output, 'y'.
circ2 = circ >> aiger_bv.repeat(wordlen=5, input='x', output='z')
# Reverse order of a word.
circ3 = circ >> aiger_bv.reverse_gate(wordlen=5, input='x', output='z')
# Sink and Source circuits (see encoding section for encoding details).
## Always output binary encoding for 15.
circ4 = aiger_bv.source(wordlen=4, value=15, name='x', signed=False)
## Absorb output 'y'
circ5 = circ >> aiger_bv.sink(wordlen=4, inputs=['y'])
# Identity Gate
circ6 = circ >> aiger_bv.identity_gate(wordlen=3, input='x')
# Combine/Concatenate words
circ7 = circ >> aiger_bv.combine_gate(
left_wordlen=3, left='x',
right_wordlen=3, right='y',
output='z'
)
# Split words
circ8 = circ >> aiger_bv.split_gate(
input='x',
left_wordlen=1, left='z',
right_wordlen=2, right='w'
)
# Select single index of circuit and make it a wordlen=1 output.
circ9 = circ >> aiger_bv.index_gate(wordlen=3, idx=1, input='x', output='x1')
```
## Bitwise Operations
- `aiger_bv.bitwise_and(3, left='x', right='y', output='x&y')`
- `aiger_bv.bitwise_or(3, left='x', right='y', output='x|y')`
- `aiger_bv.bitwise_xor(3, left='x', right='y', output='x^y')`
- `aiger_bv.bitwise_negate(3, left='x', output='~x')`
## Arithmetic
- `aiger_bv.add_gate(3, left='x', right='y', output='x+y')`
- `aiger_bv.subtract_gate_gate(3, left='x', right='y', output='x-y')`
- `aiger_bv.inc_gate(3, left='x', output='x+1')`
- `aiger_bv.dec_gate(3, left='x', output='x+1')`
- `aiger_bv.negate_gate(3, left='x', output='-x')`
- `aiger_bv.logical_right_shift(3, shift=1, input='x', output='x>>1')`
- `aiger_bv.arithmetic_right_shift(3, shift=1, input='x', output='x>>1')`
- `aiger_bv.left_shift(3, shift=1, input='x', output='x<<1')`
## Comparison
- `aiger_bv.is_nonzero_gate(3, input='x', output='is_nonzero')`
- `aiger_bv.is_zero_gate(3, input='x', output='is_zero')`
- `aiger_bv.eq_gate(3, left='x', right='y', output='x=y')`
- `aiger_bv.ne_gate(3, left='x', right='y', output='x!=y')`
- `aiger_bv.unsigned_lt_gate(3, left='x', right='y', output='x<y')`
- `aiger_bv.unsigned_gt_gate(3, left='x', right='y', output='x>y')`
- `aiger_bv.unsigned_le_gate(3, left='x', right='y', output='x<=y')`
- `aiger_bv.unsigned_ge_gate(3, left='x', right='y', output='x>=y')`
- `aiger_bv.signed_lt_gate(3, left='x', right='y', output='x<y')`
- `aiger_bv.signed_gt_gate(3, left='x', right='y', output='x>y')`
- `aiger_bv.signed_le_gate(3, left='x', right='y', output='x<=y')`
- `aiger_bv.signed_ge_gate(3, left='x', right='y', output='x>=y')`
%prep
%autosetup -n py-aiger-bv-4.7.6
%build
%py3_build
%install
%py3_install
install -d -m755 %{buildroot}/%{_pkgdocdir}
if [ -d doc ]; then cp -arf doc %{buildroot}/%{_pkgdocdir}; fi
if [ -d docs ]; then cp -arf docs %{buildroot}/%{_pkgdocdir}; fi
if [ -d example ]; then cp -arf example %{buildroot}/%{_pkgdocdir}; fi
if [ -d examples ]; then cp -arf examples %{buildroot}/%{_pkgdocdir}; fi
pushd %{buildroot}
if [ -d usr/lib ]; then
find usr/lib -type f -printf "/%h/%f\n" >> filelist.lst
fi
if [ -d usr/lib64 ]; then
find usr/lib64 -type f -printf "/%h/%f\n" >> filelist.lst
fi
if [ -d usr/bin ]; then
find usr/bin -type f -printf "/%h/%f\n" >> filelist.lst
fi
if [ -d usr/sbin ]; then
find usr/sbin -type f -printf "/%h/%f\n" >> filelist.lst
fi
touch doclist.lst
if [ -d usr/share/man ]; then
find usr/share/man -type f -printf "/%h/%f.gz\n" >> doclist.lst
fi
popd
mv %{buildroot}/filelist.lst .
mv %{buildroot}/doclist.lst .
%files -n python3-py-aiger-bv -f filelist.lst
%dir %{python3_sitelib}/*
%files help -f doclist.lst
%{_docdir}/*
%changelog
* Thu May 18 2023 Python_Bot <Python_Bot@openeuler.org> - 4.7.6-1
- Package Spec generated
|