esolang

Quantum brainfuck

Brainfuck on qubits. Every loop test is a measurement, so looking changes what is there.

Ihope127, 2006 · Turing complete: unknown · ●●● hard · esolangs.org

Tested with 2 known programs · Test method

Run it

Expected: 0

The idea

Ihope127 made Quantum brainfuck in 2006. It is brainfuck with the cells replaced by qubits, and the tape starts with every qubit at 1. There is no + or -. A qubit changes only through quantum gates, input and measurement. A Hadamard puts it in an even superposition of 0 and 1, a controlled-V turns the phase where two neighbours are both 1, and a swap moves qubits along the tape.

Read the story

Reading a qubit measures it. A loop tests by measuring and output prints what a measurement saw, so a program is random unless it is built to be certain. This implementation keeps the whole entangled state of the tape, and its measurements draw from a fixed seed, so every run of a program replays.

The machine

CharDoes
< >Move the head
%Hadamard on the current qubit
!Controlled-V on the current qubit and the one to its right
&Swap the current qubit with the one to its right
,Read a bit into the current qubit
.Measure the current qubit and print 0 or 1
[ ]Loop while measuring the current qubit gives 1

The controlled-V matrix is diag(1, 1, 1, i). The specification says output works through a CNOT onto a fresh qubit, which leaves the printed bit entangled with the tape; for the tape that is the same as a measurement. Output is the characters 0 and 1 with nothing between them.

Example

The truth machine reads a bit into the first qubit and loops on it.

The truth machine reads a bit into the first qubit and loops on it.

A Hadamard twice is the identity, so the qubit is back at 1 and the output is certain. One Hadamard alone would give 0 or 1 with even odds.

Expected: 1

The Hello world. [%] applies the Hadamard until a measurement sees 0, so the first qubit ends at 0 however the draws fall. Its neighbour is still 1, so every & flips the bit that the next . prints. The 104 bits spell Hello, world! in ASCII.

Expected: 01001000011001010110110001101100011011110010110000100000011101110110111101110010011011000110010000100001

Patterns it teaches

Further

Quantum brainfuck builds on Brainfuck.

Nondeterministic

Brainfuck derivatives

Write your own in Run Convert a program from Quantum brainfuck

Implementation

JavaScript interpreter, source and command line

A JavaScript interpreter, used in the browser and on the command line. Id: quantum-brainfuck.

Command line

shib run quantum-brainfuck -e ',[.].' --input '0'
shib manual quantum-brainfuck

Source

Module
langs/wiki/ports.js
Interpreter
langs/wiki/quantum-brainfuck.js

Docs: Kernel instructions · The .shib format · Command line · Adding a language

Source esolangs.org (CC0) · Edit on esolangs.org