Skip to content
Book 7: Computer Systems

Book 7 · Chapter 7

Emerging technologies

§7.1§7.2Exam craftB7·7

This chapter is in the ebook.

Read it in full on cswithzak.com. Every question has its mark scheme and every program is ready to run. The free practice for each section is below, with the short code the book prints beside it.

Open in the ebook

For the whole chapter

§7.1 Artificial Intelligence

Short code B7·7·1

§7.2 Quantum computers

Short code B7·7·2

What is artificial intelligence?

Short code B7·7·10

Practice for this page of the book: the quiz, flashcards and past-paper questions for its section are listed above.

The main features of AI

Short code B7·7·11

Practice for this page of the book: the quiz, flashcards and past-paper questions for its section are listed above.

Supervised learning

Short code B7·7·12

redness = [12, 25, 38, 47, 55, 63, 71, 88]
# the labels: is each tomato ripe?
label = ["no", "no", "no", "no",
         "yes", "yes", "yes", "yes"]
best_rule = 0
best_correct = 0
for rule in range(30, 71, 10):
    correct = 0
    for i in range(len(redness)):
        if redness[i] >= rule:
            guess = "yes"
        else:
            guess = "no"
        if guess == label[i]:
            correct = correct + 1
    print("rule", rule, "->", correct, "of 8 correct")
    if correct > best_correct:
        best_correct = correct
        best_rule = rule
print("Learned: ripe if redness >=", best_rule)
for new in [42, 67]:
    print("New tomato", new, "ripe?", new >= best_rule)
Supervised learning in miniature: the program tries each rule on the labelled tomatoes, keeps the one with the most correct answers, then labels two tomatoes it has never seen.

Output

rule 30 -> 6 of 8 correct
rule 40 -> 7 of 8 correct
rule 50 -> 8 of 8 correct
rule 60 -> 7 of 8 correct
rule 70 -> 6 of 8 correct
Learned: ripe if redness >= 50
New tomato 42 ripe? False
New tomato 67 ripe? True
Run and checked before printing

Unsupervised and reinforcement learning

Short code B7·7·13

import statistics
# unlabelled data: visits to a shop last month by eight customers
visits = [2, 15, 4, 18, 3, 20, 5, 17]
centre1 = visits[0]
centre2 = visits[2]
for rnd in range(1, 4):
    group1 = []
    group2 = []
    for v in visits:
        d1 = v - centre1
        if d1 < 0:
            d1 = -d1
        d2 = v - centre2
        if d2 < 0:
            d2 = -d2
        if d1 <= d2:
            group1.append(v)
        else:
            group2.append(v)
    centre1 = statistics.mean(group1)
    centre2 = statistics.mean(group2)
    print("round", rnd, group1, group2)
print("Group centres:", centre1, "and", centre2)
Unsupervised learning in miniature: no labels, yet the customers settle into two groups after one adjustment of the centres. The groups are occasional visitors and regulars.

Output

round 1 [2, 3] [15, 4, 18, 20, 5, 17]
round 2 [2, 4, 3, 5] [15, 18, 20, 17]
round 3 [2, 4, 3, 5] [15, 18, 20, 17]
Group centres: 3.5 and 17.5
Run and checked before printing

Neural networks

Short code B7·7·14

inputs = [1, 0, 1]
weights = [3, 2, -4]
threshold = 0
total = 0
for i in range(len(inputs)):
    total = total + inputs[i] * weights[i]
    print("input", inputs[i], "x weight", weights[i], "-> total", total)
if total > threshold:
    print("Output: 1 (open the window)")
else:
    print("Output: 0 (keep it shut)")
One node of a neural network: each input is multiplied by its weight and added to the total, which is then compared with the threshold.

Output

input 1 x weight 3 -> total 3
input 0 x weight 2 -> total 3
input 1 x weight -4 -> total -1
Output: 0 (keep it shut)
Run and checked before printing

How a neural network learns

Short code B7·7·15

a = [0, 0, 1, 1]
b = [0, 1, 0, 1]
target = [0, 0, 0, 1]
w1 = 0
w2 = 0
threshold = 1
rnd = 0
print("round mistakes w1 w2 threshold")
while True:
    rnd = rnd + 1
    mistakes = 0
    for i in range(4):
        out = 0
        if a[i] * w1 + b[i] * w2 >= threshold:
            out = 1
        error = target[i] - out
        if error != 0:
            mistakes = mistakes + 1
            w1 = w1 + error * a[i]
            w2 = w2 + error * b[i]
            threshold = threshold - error
    print(rnd, mistakes, w1, w2, threshold)
    if mistakes == 0:
        break
Training one node to act as an AND gate: after each mistake the weights and threshold move towards the right answer, until a whole round has no mistakes.

Output

round mistakes w1 w2 threshold
1 1 1 1 0
2 3 2 1 1
3 3 2 1 2
4 2 2 2 2
5 1 2 1 3
6 0 2 1 3
Run and checked before printing
# 2 inputs -> 2 hidden nodes -> 1 output
def node(values, weights, threshold):
    total = 0
    for i in range(len(values)):
        total = total + values[i] * weights[i]
    if total >= threshold:
        return 1
    else:
        return 0

for a in range(2):
    for b in range(2):
        h1 = node([a, b], [1, 1], 1)
        h2 = node([a, b], [1, 1], 2)
        out = node([h1, h2], [1, -1], 1)
        print(a, b, "hidden:", h1, h2, "output:", out)
A hidden layer makes XOR possible: the first hidden node acts as OR, the second as AND, and the output node fires only when OR is 1 and AND is 0.

Output

0 0 hidden: 0 0 output: 0
0 1 hidden: 1 0 output: 1
1 0 hidden: 1 0 output: 1
1 1 hidden: 1 1 output: 0
Run and checked before printing

Applications of AI

Short code B7·7·16

Practice for this page of the book: the quiz, flashcards and past-paper questions for its section are listed above.

Using AI in a given context

Short code B7·7·17

Practice for this page of the book: the quiz, flashcards and past-paper questions for its section are listed above.

Fairness and bias

Short code B7·7·18

Practice for this page of the book: the quiz, flashcards and past-paper questions for its section are listed above.

Ethics and society

Short code B7·7·19

Practice for this page of the book: the quiz, flashcards and past-paper questions for its section are listed above.

Jobs, safety and the environment

Short code B7·7·20

Practice for this page of the book: the quiz, flashcards and past-paper questions for its section are listed above.

Quantum computers

Short code B7·7·21

for n in [2, 3, 10, 20]:
    print(n, "bits or qubits:", 2 ** n, "patterns")
Each extra bit or qubit doubles the number of patterns. A classical register holds one of them at a time; the same number of qubits can hold a combination of them all until measured.

Output

2 bits or qubits: 4 patterns
3 bits or qubits: 8 patterns
10 bits or qubits: 1024 patterns
20 bits or qubits: 1048576 patterns
Run and checked before printing

Advantages and disadvantages of quantum computers

Short code B7·7·22

Practice for this page of the book: the quiz, flashcards and past-paper questions for its section are listed above.

Past-paper practice

Short code B7·7·90

Practice for this page of the book: the quiz, flashcards and past-paper questions for its section are listed above.

Workbook: Emerging technologies

Short code W7·7 · Computer Systems Workbook

The write-in workbook for this chapter has drills, exam-style practice, trace tables, fix-the-mistake and mark-it-yourself pages. Its full mark schemes and an interactive version arrive here with the chapter.

Ask Z about Emerging technologies
Buy ZAK's books — WhatsApp the books desk: Buy the booksPrint or ebook