CS3102 Theory of Computation - - PowerPoint PPT Presentation
CS3102 Theory of Computation - - PowerPoint PPT Presentation
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
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
𝑀𝑃𝑃𝐿𝑉𝑄𝑙
6
1 1 1 1 𝑙 = 3 1 First 2𝑙 bits of input Considered as a bitstring Last 𝑙 bits of input Considered as an index
x: i:
Theorem
7
There is a NAND-Cricuit that computes 𝑀𝑃𝑃𝐿𝑉𝑄𝑙: 0,1 2𝑙+𝑙 → {0,1} Moreover, the number of gates required is at most 4 ⋅ 2𝑙
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
8
1 1 1 1 1
𝑦: 𝑗: 𝑦: 𝑗:
1 1
Defining 𝑀𝑃𝑃𝐿𝑉𝑄𝑙
9
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)
Base Case
10
Next Step
LOOKUP2
11
LOOKUP3 and 4
Counting Gates
12
Show this uses at most 4 ⋅ 2𝑙 − 4 gates (lines of code)
Counting Gates
13
Show this uses at most 4 ⋅ 2𝑙 − 4 gates (lines of code)
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
15
Input Output 000 001 010 1 011 100 1 101 1 110 111
We will have one variable to represent each possible input. We'll do a lookup with the actual input to select the proper output
Consider the function 𝑔: 0,1 3 → {0,1}
Straightline Code for F
16
Input Output 000 001 010 1 011 100 1 101 1 110 111
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
20
Input Output 000 001 010 1 011 100 1 101 1 110 111
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 Note: This can be improved to 𝑑 ⋅ 𝑛 ⋅
2𝑜 𝑜 (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
27
Variable Value
Simulating XOR
28
Variable Value a b 1 u 1 v 1 w return 1
Defining EVAL
29
Input: bit string representing a program (first 𝑇(𝑡) bits) plus input values (remaining 𝑜 bits) Output: the result of running the represented program
- n the provided input, or 𝑛 0's if there's a "compile
error"
𝐹𝑊𝐵𝑀𝑡,𝑜,𝑛: 0,1 𝑇 𝑡 +𝑜 → 0,1 𝑛
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])
30
Variable Number a b 1 temp1 2 temp2 3 return 4 (2,0,0) (3,1,1) (4,2,3) (2,1,[(2,0,0),(3,1,1),(4,2,3)])
XOR to bits
31
Variable Number 𝑜 = 𝑛 = 𝑡 = Total bits =
XOR to bits
32
Variable Number a b 1 u 2 v 3 w 4 return 5 𝑜 = 2 𝑛 = 1 𝑡 = 4 Total bits = 3 [numbers per line] ⋅ 3 [bits per number] ⋅ 4[lines] + 6 [length of 𝑜 + 𝑛]
How big is this?
1. Number each variable 2. Represent each line as 3 numbers (outvar, in1, in2) 3. Represent program as (n,m,[Lines])
33
⌈log2 3𝑡⌉ bits each
𝑇 𝑡 ≤ 4𝑡⌈log2 3𝑡⌉ ℓ = ⌈log2 3𝑡⌉
3𝑡 ⋅ ⌈log2 3𝑡⌉ bits 2⌈log2 𝑡⌉ bits
Defining EVAL
34
Input: bit string representing a program (first 𝑇(𝑡) bits) plus input values (remaining 𝑜 bits) Output: the result of running the represented program
- n the provided input, or 𝑛 0's if there's a "compile
error"
𝐹𝑊𝐵𝑀𝑡,𝑜,𝑛: 0,1 𝑇 𝑡 +𝑜 → 0,1 𝑛
Defining the EVAL function
35
Variable Value 1 2 3 4 5 Representation: (2, 0, 1), (3, 0, 2), (4, 1, 2), (5, 3, 4) Input: 0, 1
𝑜 = 2 𝑛 = 1
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:
– Numbers 𝑜, 𝑛, 𝑡, 𝑢 representing the number of inputs, outputs, variables, and lines respectively – 𝑀, a list of triples representing the program – A string 𝑦 to be given as input to the program
- Output:
– Evaluation of the program represented by 𝑀 when run
- n input 𝑦
37
Let 𝑈 be table of size 𝑢 For 𝑗 in range(𝑜): 𝑈 = UPDATE(𝑈, 𝑗, 𝑦[𝑗]) For (𝑗,𝑘,𝑙) in 𝑀: 𝑏 = GET(𝑈, 𝑘) 𝑐 = GET(𝑈, 𝑙) 𝑈 = UPDATE(𝑈, 𝑗, NAND(𝑏,𝑐)) For 𝑗 in range(𝑛): 𝑍[𝑗] = GET(𝑈, 𝑢 − 𝑛 + 𝑗) Return 𝑍
EVAL in NAND
- Next we implement 𝐹𝑊𝐵𝑀𝑡,𝑜,𝑛 using NAND
38
𝐻𝐹𝑈(𝑈, 𝑗)
- Get the bit at “row” 𝑗 of 𝑈
- Look familiar?
- How many gates to implement?
39
UPDATE
40
- 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 𝑉𝑄𝐸𝐵𝑈𝐹ℓ: 0,1 𝑡ℓ+ℓ+1 → 0,1 2ℓ
UPDATE pseudocode
For 𝑘 in range(2ℓ): 𝑏 = 𝐹𝑅𝑉𝐵𝑀𝑇
𝑘(𝑗)
𝑜𝑓𝑥𝑈[𝑘] = 𝐽𝐺(𝑏, 𝑐, 𝑈[𝑘]) Return 𝑜𝑓𝑥𝑈
41
Runs 2ℓ times
Conclusion
42