CS3102 Theory of Computation www.cs.virginia.edu/~njb2b/cstheory/s2020 Warm up: Software can be reconfigured, hardware cannot. When we build hardware, how do we decide what to implement? If you found the fitbit, the owner is down front, and is upset about all her wasted steps from the past 2 days.
2
Logistics • Exercise 2 due Tuesday • Quiz 3 released Friday • Exercise 3 is out this weekend – Last “regular - sized” exercise before midterm – There will be a “tiny” exercise 4 due the Tuesday before the midterm 3
Last Time • Boolean Circuits as a model of computing • Straightline Programs as a model of computing • Proved NAND-Straightline = NAND-Circ = AON-Circ = AON-straightline 4
Lookup • Indexing into a bitstring • The 𝑀𝑝𝑝𝑙𝑣𝑞 function of order 𝑙 : 𝑀𝑃𝑃𝐿𝑉𝑄 𝑙 : 0,1 2 𝑙 +𝑙 → 0,1 Defined such that for 𝑦 ∈ 0,1 2 𝑙 , 𝑗 ∈ 0,1 𝑙 : 𝑀𝑃𝑃𝐿𝑉𝑄 𝑙 𝑦, 𝑗 = 𝑦 𝑗 5
𝑀𝑃𝑃𝐿𝑉𝑄 𝑙 𝑙 = 3 i : x : 0 1 0 0 1 1 1 0 0 1 0 Last 𝑙 bits of input First 2 𝑙 bits of input Considered as an index Considered as a bitstring 6
Theorem There is a NAND-Cricuit that computes 𝑀𝑃𝑃𝐿𝑉𝑄 𝑙 : 0,1 2 𝑙 +𝑙 → {0,1} Moreover, the number of gates required is at most 4 ⋅ 2 𝑙 7
Proof idea • Consider index 𝑗 • If the first bit of 𝑗 is 0, then the bit we're looking for is in the first half of 𝑦 • Do lookup for 𝑙 − 1 𝑗 : 𝑦 : 0 1 0 0 1 1 1 0 0 1 0 𝑗 : 𝑦 : 0 1 0 0 1 0 8
Defining 𝑀𝑃𝑃𝐿𝑉𝑄 𝑙 For 𝑙 ≥ 2, 𝑀𝑃𝑃𝐿𝑉𝑄 𝑙 (𝑦 0 , … , 𝑦 2 𝑙 −1 , 𝑗 0 , … , 𝑗 𝑙−1 ) is equal to: 𝐽𝐺(𝑗 0 , 𝑀𝑃𝑃𝐿𝑉𝑄 𝑙−1 𝑦 2 𝑙−1 , … , 𝑦 2 𝑙 −1 , 𝑗 1 , … , 𝑗 𝑙−1 , 𝑀𝑃𝑃𝐿𝑉𝑄 𝑙−1 (𝑦 0 , … , 𝑦 2 𝑙−1 −1 , 𝑗 1 , … , 𝑗 𝑙−1 ) 9
Base Case Next Step 10
LOOKUP2 LOOKUP3 and 4 11
Counting Gates Show this uses at most 4 ⋅ 2 𝑙 − 4 gates (lines of code) 12
Counting Gates Show this uses at most 4 ⋅ 2 𝑙 − 4 gates (lines of code) 13
Computing Every Finite Function • Next we'll show that NAND is “universal” • Any finite function can be computed by some NAND-straightline program (equivalently, a NAND-circuit) 14
Idea Consider the function 𝑔: 0,1 3 → {0,1} Input Output We will have one variable to represent each 000 0 possible input. We'll do a lookup with the 001 0 actual input to select the proper output 010 1 011 0 100 1 101 1 110 0 111 0 15
Straightline Code for F Input Output 000 0 001 0 010 1 011 0 100 1 101 1 110 0 111 0 16
Getting 0 and 1 17
Getting 0 and 1 18
Computing any function • Make a variable to represent each possible input • Assign its value to match the correct output • Use LOOKUP to select the proper output for the given input 19
Straightline Code for F Input Output 000 0 001 0 010 1 011 0 100 1 101 1 110 0 111 0 20
How many gates? • How many gates does this construction take? You can compute any finite function 𝑔: 0,1 𝑜 → 0,1 𝑛 with a NAND Circuit using no more than 𝑑 ⋅ 𝑛 ⋅ 2 𝑜 gates 2 𝑜 Note: This can be improved to 𝑑 ⋅ 𝑛 ⋅ 𝑜 (theorem 4.16 in TCS) 21
Counting gates 1. Create variables for each input 2. Assign 0,1 to each input 3. Do the LOOKUP 22
23
What does this mean? • Your laptop is a 64-bit machine. Given enough transistors, it can compute any function 𝑔: 0,1 64 → 0,1 64 24
Any to Every • Previous theorem: – We can compute ANY 𝑜 -bit function using circuits/straightline programs • What we want: – A machine that can compute EVERY 𝑜 -bit function • How do we do this?: – Define a function that "simulates" programs – Write a program that gives the same answer as a given program of 𝑜 inputs, 𝑛 outputs, and 𝑡 lines 25
How are programs run? • Have a table of variables • Execute code in sequence • Update values in table • Return a value from the table 26
Simulating XOR Variable Value 27
Simulating XOR Variable Value a 0 b 1 u 1 v 1 w 0 return 1 28
Defining EVAL 𝐹𝑊𝐵𝑀 𝑡,𝑜,𝑛 : 0,1 𝑇 𝑡 +𝑜 → 0,1 𝑛 Input: bit string representing a program (first 𝑇(𝑡) bits) plus input values (remaining 𝑜 bits) Output: the result of running the represented program on the provided input, or 𝑛 0's if there's a "compile error" 29
Programs as Bits • To evaluate a program with another program, we need to convert the first program into bits 1. Number each variable (first n go to input, last m to outputs) 2. Represent each line as 3 numbers (outvar, in1, in2) 3. Represent program as (n,m,[Lines]) Variable Number a 0 (2,0,0) b 1 (3,1,1) temp1 2 (4,2,3) temp2 3 (2,1,[(2,0,0),(3,1,1),(4,2,3)]) return 4 30
XOR to bits Variable Number 𝑜 = 𝑛 = 𝑡 = Total bits = 31
XOR to bits Variable Number a 0 b 1 u 2 𝑜 = 2 v 3 𝑛 = 1 w 4 𝑡 = 4 return 5 Total bits = 3 [numbers per line] ⋅ 3 [bits per number] ⋅ 4[lines] + 6 [length of 𝑜 + 𝑛 ] 32
How big is this? 1. Number each variable ⌈log 2 3𝑡⌉ bits each 2. Represent each line as 3 numbers (outvar, in1, in2) 3𝑡 ⋅ ⌈log 2 3𝑡⌉ bits 3. Represent program as (n,m,[Lines]) 2⌈log 2 𝑡⌉ bits 𝑇 𝑡 ≤ 4𝑡⌈log 2 3𝑡⌉ ℓ = ⌈log 2 3𝑡⌉ 33
Defining EVAL 𝐹𝑊𝐵𝑀 𝑡,𝑜,𝑛 : 0,1 𝑇 𝑡 +𝑜 → 0,1 𝑛 Input: bit string representing a program (first 𝑇(𝑡) bits) plus input values (remaining 𝑜 bits) Output: the result of running the represented program on the provided input, or 𝑛 0's if there's a "compile error" 34
𝑜 = 2 Defining the EVAL function 𝑛 = 1 Variable Value Representation: 0 (2, 0, 1), (3, 0, 2), (4, 1, 2), 1 (5, 3, 4) 2 Input: 0, 1 3 4 5 35
Psuedocode for EVAL 𝑈 • Table 𝑈 : – holds variables and their values • 𝐻𝐹𝑈(𝑈, 𝑗) – Returns the bit of 𝑈 associated with variable 𝑗 • 𝑉𝑄𝐸𝐵𝑈𝐹(𝑈, 𝑗, 𝑐) – Returns a new table such that variable 𝑗 's value has been changed to 𝑐 36
Psuedocode for EVAL • Input: Let 𝑈 be table of size 𝑢 Numbers 𝑜 , 𝑛 , 𝑡 , 𝑢 For 𝑗 in range( 𝑜 ): – representing the number of 𝑈 = UPDATE( 𝑈 , 𝑗 , 𝑦[𝑗] ) inputs, outputs, variables, For ( 𝑗 , 𝑘 , 𝑙 ) in 𝑀 : and lines respectively 𝑏 = GET( 𝑈 , 𝑘 ) 𝑀 , a list of triples – 𝑐 = GET( 𝑈 , 𝑙 ) representing the program 𝑈 = UPDATE( 𝑈 , 𝑗 , NAND( 𝑏 , 𝑐 )) A string 𝑦 to be given as input – For 𝑗 in range( 𝑛 ): to the program 𝑍 [ 𝑗 ] = GET( 𝑈 , 𝑢 − 𝑛 + 𝑗 ) • Output: Return 𝑍 – Evaluation of the program represented by 𝑀 when run on input 𝑦 37
EVAL in NAND • Next we implement 𝐹𝑊𝐵𝑀 𝑡,𝑜,𝑛 using NAND 38
𝐻𝐹𝑈(𝑈, 𝑗) • Get the bit at “row” 𝑗 of 𝑈 • Look familiar? • How many gates to implement? 39
UPDATE 𝑉𝑄𝐸𝐵𝑈𝐹 ℓ : 0,1 𝑡 ℓ +ℓ+1 → 0,1 2 ℓ • To change index 𝑗 of table 𝑈 to bit 𝑐 • For every index except 𝑗 , return the same value • For index 𝑗 , return b instead • Define 𝐹𝑅𝑉𝐵𝑀 𝑘 : 0,1 ℓ → {0,1} which returns 1 if the input binary number is equal to j Note: 𝐹𝑅𝑉𝐵𝑀 𝑘 can be done in 𝑑 ⋅ ℓ gates 40
UPDATE pseudocode For 𝑘 in range( 2 ℓ ): 𝑏 = 𝐹𝑅𝑉𝐵𝑀𝑇 𝑘 (𝑗) Runs 2 ℓ times 𝑜𝑓𝑥𝑈[𝑘 ] = 𝐽𝐺(𝑏, 𝑐, 𝑈[𝑘]) Return 𝑜𝑓𝑥𝑈 41
Conclusion 42
Recommend
More recommend