liveBook · Manning

Fibonacci sequence

As explained in Building Quantum Software with Python

The Fibonacci sequence is the sequence whose successive terms are formed by adding the two preceding terms, conventionally starting with 0 and 1.

The Fibonacci sequence starts with 0 and 1; each later term equals the sum of the two preceding terms. In quantum outcome selection, the same recurrence counts binary strings that contain no consecutive 1s. section 5.4

A recursive implementation mirrors the definition:

  • Return n directly when n is 0 or 1. section 5.4
  • Otherwise return recursive_fib(n - 1) + recursive_fib(n - 2). section 5.4
  • The first ten values are 0, 1, 1, 2, 3, 5, 8, 13, 21, and 34. section 5.4

Counting valid binary outcomes

For three-bit strings, the valid outcomes are 000, 001, 010, 100, and 101. Their count is 5: strings beginning with 0 extend a valid two-bit string, while strings beginning with 1 must continue with 0 and then extend a valid one-bit string. section 5.4

def recursive_fib(n):
    assert n >= 0
if n <= 1:
        return n
    else:
        return recursive_fib(n - 1) + recursive_fib(n - 2)

Inspect the recursive Python implementation that directly encodes the two-term recurrence. section 5.4

How Building Quantum Software with Python covers it

How deep it goes
Explains the recurrence, works through three-bit outcomes, implements recursion, and connects consecutive-term ratios to the golden ratio.
Examples
Uses valid three-bit strings, a recursive Python function, the first ten values, and quantum states for one, two, and three qubits.
What it assumes
Assumes basic binary strings and Python recursion while deriving the counting recurrence through concrete cases.
What you can do after
Builds a recursive Fibonacci calculator and recognizes Fibonacci counts in constrained binary-outcome problems.
How it connects
Connects Fibonacci numbers with binary strings, quantum computation, quantum oracles, and the golden ratio.

Also explained in

Compare how 6 books explain Fibonacci sequence →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building Quantum Software with Python. The section links point to the parts of the book it draws on.