\documentclass[]{article}
\usepackage{amsmath,graphicx,supertabular,hyperref,tabularx,ifthen}
\title{TIP \#263: Quantum Tcl}
\date{April 01, 2006}
\author{Lars Hellström}
\urlstyle{sf}
\setlength{\parskip}{1ex}
\setlength{\parindent}{0pt}
\def\tipversion$#1${\texttt{\$#1\$}}
\def\tiplangle#1{\ensuremath{<}}
\def\tiprangle#1{\ensuremath{>}}
\def\tipbar#1{\ensuremath{|}}
\def\tipmail#1#2{\(\langle\){\small\expandafter\url{#1@#2}}\(\rangle\)}
\ifx\pdfoutput\undefined
\newcommand{\tipimage}[2]{\typeout{Make sure you download #1.eps}\ifthenelse{\lengthtest{0.8\textwidth>#2}\and\lengthtest{0pt<#2}}{\includegraphics{#1.eps}}{\includegraphics[width=0.8\textwidth]{#1.eps}}}
\newcommand{\tipxref}[1]{}
\newcommand{\tipxrefend}{}
\else
\newcommand{\tipimage}[2]{\typeout{Make sure you create #1.pdf}\ifthenelse{\lengthtest{0.8\textwidth>#2}\and\lengthtest{0pt<#2}}{\includegraphics{#1.pdf}}{\includegraphics[width=0.8\textwidth]{#1.pdf}}}
\pdfcatalog{/PageMode /UseOutlines}
\newcommand{\tipxref}[1]{\pdfannotlink attr {/C [0.5 0.5 1.0] /Border [0 0 1]} goto name {#1}}
\newcommand{\tipxrefend}{\pdfendlink}
\fi
\newenvironment{tipabstract}{\begin{abstract}}{\end{abstract}}
\begin{document}\maketitle
\begin{center}\begin{tabularx}{\linewidth}{|r@{: }X|}\hline
\textbf{TIP \#263}&\textbf{Quantum Tcl}\\\hline
Author&

Lars Hellström \tipmail{Lars.Hellstrom}{residenset.net}
\\
Created&Saturday, \(\text{1}^{\text{st}}\) April 2006\\
Type&Project\\
Tcl Version&\(9.2\)\\
State&Draft\\
Vote&Pending\\
Version&\tipversion$Revision: 1.2 $\\
Post History&\\
\hline\end{tabularx}\end{center}
\thispagestyle{empty}\pagestyle{empty}
\begin{tipabstract}

A new Tcl command \textbf{qubit} is proposed. This command makes it possible to handle quantum information in the form of qubits.
\end{tipabstract}
\tableofcontents\setcounter{page}{0}\clearpage\pagestyle{plain}

\section{Rationale}

As stated in \cite{tip131}, what Tcl needs in order to succeed in the marketplace is a feature that no other programming language provides, a ``killer app'' as it were. The Tk toolkit, Expect, cross-platform portability, starkits, tkcon, and excellent embed/extend-ability with respect to other languages are all well and good, but they have clearly failed to push Tcl usage to the point of having critical mass. The \textbf{qubit} command makes it possible to achieve an exponential speedup for important problems and should therefore provide a powerful enough incentive that even Perl programmers would be compelled to switch languages.

\section{Background}

Quantum computing makes use of phenomena in quantum mechanics to, at each time step, carry out an exponential amount of work using only a linear amount of hardware. The way this maps onto physical reality is pretty mind-boggling, but for the programmer it is sufficient to think of the Quantum Processing Unit (QPU) as an extremely powerful but somewhat specialised coprocessor and leave it at that. (Chances are anyway that the QPU isn't physically located in your desktop computer, as they tend to involve lots of lasers, magnets, vacuum chambers, liquid nitrogen cooling, etc.)

Quantum information display an interesting duality in that it is analog during a computation, but becomes digital as soon as one measures it. (Wave/particle duality, in case anyone came to think of that, is the kind of ``mapping onto physical reality'' issue that will not be treated here.) This makes the design of quantum algorithms somewhat different from the design of classical algorithms, in a manner similar to that in which the design of analog electronic circuits is different from the design of digital electronic circuits, as one must often work out the numbers rather than rely on a discrete case-by-case analysis. Another curious feature is that all fully quantum operations must be reversible, which in particular has the effect that quantum information can neither be duplicated nor (in the absence of measurements) destroyed, only rearranged. There is in particular no quantum analogue to assignments such as [set a \$b], since not only need this copy the value of b, it also irrecoverably destroys the old value of a; the closest one gets to such an assignment is exchanging the values of a and b.

For more information on quantum computing in general, see e.g.:
\begin{itemize}
\item{}
Wikipedia article ``Quantum computer'' [\url{http://en.wikipedia.org/wiki/Quantum_computer}].
\end{itemize}
\begin{description}
\item[* J. Gruska]
Quantum Computing (1999), ISBN 0-07-709503-0, [\url{http://www.fi.muni.cz/usr/gruska/quantum/}].
\end{description}

\section{Specification}

The quantum computing model supported by the \textbf{qubit} command is that of \textit{quantum bits} (more commonly called \textit{qubits}) and \textit{quantum boolean circuits}. While other more fancy models such as ``Quantum Turing Machines'' exist, this is generally considered to be the most realistic model, and it is also the one most closely related to the number-of-gates complexity measure for classical computing. Should it in the future prove desirable to support also some other model, then one may do so through a separate command.

In this model, a quantum state of N qubits is completely specified by a set of 2**N complex numbers, usually known as \textit{probability amplitudes} (they are not probabilities as such, but they do completely determine the probabilities for various events). Many different bases are possible, but in the standard (also known as the computational) basis each of these amplitudes corresponds to a particular assignment of 0s and 1s to the qubits. Operations on a quantum state can be understood as operations on the vector of amplitudes.

The \textbf{qubit} command has the five subcommands.

\subsection{The 'new' Subcommand}

\begin{quote}
\textbf{qubit new} ?\textit{-option value} ?...??
\end{quote}

Allocate/create a new qubit, and return a handle for the new qubit that identifies it in subsequent operations, or throw an error if allocation/creation failed (possible causes include, but are not limited to, lack of resources on the hardware side and user having insufficient permissions). New qubits are not entangled with any of the old ones, but their state is otherwise unspecified.

The options are meant as a means for supplying extra information about the new qubit, such as for example whether it is being protected from decoherence by a scheme of quantum error correction codes (the Tcl core can easily implement such features on platforms where the C level APIs only provide raw qubits); however at present no options are defined.

\subsection{The 'operate' Subcommand}

\begin{quote}
\textbf{qubit operate} \textit{gate id0} ?\textit{id1} ?...??
\end{quote}

Perform a quantum operation (the \textit{gate}) on one or more qubits (specified using the handles \textit{id0}, \textit{id1}, etc.). Returns the operation actually applied.

In the interest of generality, gates are specified as unitary matrices (this is a universal representation for quantum gates), or more concretely as lists of lists of lists of doubles. The innermost lists must have length 2 and encode the real (index 0) and imaginary (index 1) parts of an element of the matrix. Indices in the middle list level select a column and indices in the outer list level consequently a row. Put another way,
\begingroup\small\begin{verbatim}
 lindex $gate $i $j 0 ; # Returns Re gate(i,j)
 lindex $gate $i $j 1 ; # Returns Im gate(i,j)
\end{verbatim}
\endgroup

The row/column index corresponding to the \textit{id0} qubit having value \$r0, the \textit{id1} qubit having value \$r1, etc. is \$r0*(2**0) + \$r1*(2**1) + ... A \textit{gate} for operating on \textit{n} qubits must thus have side 2**\textit{n}. Columns correspond to qubit states before the operation and rows correspond to qubit states after the operation.

An error is thrown if the number of qubit arguments does not match the side of the \textit{gate} matrix, if not all \textit{idN} arguments are qubit handles, if some qubit occurs twice in the list, and if \textit{gate} is not a proper matrix (too many or too few elements in some list, elements not recognised as doubles, etc.).

An error is \textit{not} thrown if the \textit{gate} is not unitary. In general the operation actually applied has to be supported by the available hardware, so the \textbf{qubit operate} command (or some lower level interface) should determine which supported operation most closely approximates the specified \textit{gate} and apply that instead. The user can check what was done (up to numeric precision) by inspecting the return value. Using a return value from \textbf{qubit operate} as the \textit{gate} for another call should result in the exact same operation being carried out.

\subsection{The 'measure' Subcommand}

\begin{quote}
\textbf{qubit measure} \textit{id}
\end{quote}

Measures a qubit with respect to the standard basis. Returns 0 or 1 depending on the resulting state.

\textit{Note} that measuring a qubit changes the quantum state to one in which that qubit has a pure value. If other qubits are initially entangled with the one being measured, then these will also be affected by this operation. Measuring a qubit causes it to be disentangled from all other qubits (or perhaps entangles the state of the entire universe with the qubit, depending on your philosophical point of view).

\subsection{The 'dispose' Subcommand}

\begin{quote}
\textbf{qubit dispose} \textit{id}
\end{quote}

Frees/deallocates a qubit, returning it to whatever pool of resources \textbf{qubit new} got it from, but before doing that the qubit is measured to safely disentangle it from any remaining qubits. The return value is 0 or 1 as for the corresponding \textbf{qubit measure}.

\subsection{The 'names' Subcommand}

\begin{quote}
\textbf{qubit names}
\end{quote}

This is an instrospection command. It returns a list of all qubit handles currently available in this interpreter.

\subsection{Future Expansion}

Other subcommands may be added in the future, but this set is complete for single interpreter algorithms.

\section{Examples}

The syntax of \textbf{qubit operate} was chosen to facilitate the creation of aliases for common gates, as this should make programs more readable. An alias for the CNOT (conditional not) gate can be created as
\begingroup\small\begin{verbatim}
 interp alias {} CNOT {} qubit operate {
    { {1 0} {0 0} {0 0} {0 0} }
    { {0 0} {0 0} {0 0} {1 0} }
    { {0 0} {0 0} {1 0} {0 0} }
    { {0 0} {1 0} {0 0} {0 0} }
 }
\end{verbatim}
\endgroup

after which one can use the command
\begingroup\small\begin{verbatim}
 CNOT $control $target
\end{verbatim}
\endgroup

The more significant \$target qubit is negated if the \$control qubit is 1 but left alone otherwise.

Another standard gate is the Hadamard gate, which can be defined as follows.
\begingroup\small\begin{verbatim}
 set rsqrt2 [list [expr {1/sqrt(2)}] 0] ; # Reciprocal square root of  2.
 interp alias {} Hadamard {} qubit operate [
   list [list $rsqrt2 $rsqrt2] [list $rsqrt2 [list [expr -sqrt(0.5)]  0]]
 ]
\end{verbatim}
\endgroup

The Hadamard gate is used to create states that are uniform superpositions of 0s and 1s. A simple application of that is the following random bit generator.
\begingroup\small\begin{verbatim}
 proc randombit {} {
     set id [qubit new]        ; # Allocate qubit
     qubit measure $id         ; # Make pure 0 or pure 1
     Hadamard $id              ; # Make an equal mix of 0 and 1
     return [qubit dispose $id]; # Measure and clean up
 }
\end{verbatim}
\endgroup

Note that this (provided, of course, that one believes in the standard interpretation of quantum mechanics) is not a psuedo-random bit generator, but a truly random bit generator. Even if the Hadamard gate would be slightly off (unlikely, as this is a very standard gate, but possible) this would not affect the essential randomness of the bits produced, but only the exact probability.

A third type of elementary gate is the phase shift gate, which changes the phase (but not the size) of some probability amplitude. To change the phase of the 1 amplitude of a qubit \$id by the angle \$phi, one would use the command
\begingroup\small\begin{verbatim}
 qubit operate [list {{1.0 0.0} {0.0 0.0}} [list {0.0 0.0} [
     list [expr {cos($phi)}] [expr {sin($phi)}]
 ]]] $id
\end{verbatim}
\endgroup

Phase changes do not change the probability distribution for any qubit measurement, but they do affect the state in ways that can lead to different probabilities further on, and thus illustrate the fact that there is more to a quantum state than the probability distribution it gives rise to here and now. As a concrete example of this, assuming \$id is a qubit, and with aliases as above, the script:
\begingroup\small\begin{verbatim}
 set before [qubit measure $id]
 Hadamard $id
 Hadamard $id
 set after [qubit measure $id]
 expr {$before == $after}
\end{verbatim}
\endgroup

will with probability 1 produce the result 1, whereas the script:
\begingroup\small\begin{verbatim}
 set before [qubit measure $id]
 Hadamard $id
 qubit operate {{{1 0} {0 0}} {{0 0} {-1 0}}} $id
 Hadamard $id
 set after [qubit measure $id]
 expr {$before == $after}
\end{verbatim}
\endgroup

will with probability 1 produce the result 0. The only difference is the 180 degrees phase shift of the 1 amplitude in the explicit \textbf{qubit operate} command, which transforms one state with equal probabilities for 0 and 1 to another state with equal probabilities for 0 and 1!

\section{Rejected Alternatives}

One might expect that a truly Quantum Tcl would keep quantum information as ``first class data'', i.e., in Tcl\_Objs to be passed around by value rather than as qubits that can only be passed around by name, but that is impossible (unless one goes to such lengths as to run the entire Tcl process in a QPU, which again will probably never be possible) due to a fundamental incompatibility between the laws of quantum mechanics and Tcl's Everything Is A String principle.

Beginning with the EIAS side, one may observe that for a quantum state to be encodable into a Tcl\_Obj, it must be serializable --- there must be a way of generating a string that completely encodes the state. Since quantum mechanics does not permit extracting that much information about a quantum state, there are only two options: either everything is kept within the QPU (not realistic), or nothing is kept in the QPU. In the latter case, one loses entirely the advantage of quantum computation, so it is rather pointless.

On the quantum side, one may observe that most of the things that are routinely done to Tcl\_Objs are simply impossible to do to quantum information. The fundamental problem here is that Tcl\_Objs must be duplicatable, whereas it is a theorem in quantum mechanics that quantum states cannot be duplicated (the ``No cloning'' theorem). Somewhat related is the problem that quantum information can only be read (used as input to some operation) once, whereas a Tcl\_Obj can be written once but read an unlimited number of times.

\section{Security Implications}

As the \textbf{qubit} command only manipulates data and cannot be used for any form of communication, it may in principle be made available also in safe interpreters. However since \textbf{qubit new} seizes a global resource that can be expected to be in limited supply on a system, it is probably better to be safe than sorry, and therefore the \textbf{qubit} command shall initially be hidden in a safe interpreter.

Omitting the command entirely and instead alias all qubit operations to the \textbf{qubit} command of the parent interpreter is \textit{not} a good idea, as the quick (but sloppy) implementation of this would allow untrusted code evaluated in the safe interpreter access also to the qubits of the parent.

It should be noted that the easy access to quantum computing that this command provides would have significant implications for the security of many external systems. Such issues are outside the scope of this TIP.

\section{Future Extensions}

Besides quantum algorithms, many interesting applications of quantum information processing involves communication through the means of a quantum state shared by different parties. While fast long distance qubit transportation is physically made possible by means of quantum teleportation (which really isn't as fancy as it sounds --- basically it amounts to a combination of the old TV chef trick of having prepared something in advance, in this case physically transferring a qubit, and the patchfile trick of only transmitting a diff against what was physically distributed), there are currently no standardised protocols for this, and until the time that there is there probably isn't much point in specifying some \textbf{qubit socket} command for Tcl either. It may however be observed that \textit{non-open} commercial systems [\url{http://www.magiqtech.com/}] transmitting quantum information over long distances are available today.

While transferring qubits between different machines obviously present some technical problems, it may seem that transferring qubits between different interpreters in the same process should at least be straightforward, but the presence of multiple threads in the process introduce complications also for this case. Concretely, transferring a qubit from one thread to another will in general cause these threads to become entangled! In order to not make thread maintenance even more complicated by introducing the concept of quantum deadlock due to thread tangles, this TIP does not treat the subject of a mechanism for transferring qubits between interpreters.

\section{Reference Implementation}

A Tcl level emulation of the \textbf{qubit} command (minus some error checking, but also not requiring a QPU) is available as SF patch no 1462755 [\url{http://sf.net/tracker/?func=detail&aid=1462755&group_id=10894&atid=310894}]. This emulation uses the standard Tcl rand() function for generating random numbers, so it is not cryptographically safe. Also note that it internally uses of some tcllib packages, which must therefore be available.

No C implementation exists at present, but creating one is a simple matter of programming (SMOP). In particular, since the details of the command implementations for the foreseeable future almost surely will have some dependence on the particular hardware present, it seems appropriate to assign to each subcommand an entry in the internal stubs table and then simply have the main \textit{Tcl\_QubitObjCmd} call each as appropriate.
\begingroup\small\begin{verbatim}
   int
   Tcl_QubitObjCmd(
       ClientData clientData,      /* Might be used. */
       Tcl_Interp *interp,         /* Current interpreter. */
       int objc,                   /* Number of arguments. */
       Tcl_Obj *CONST objv[])      /* Argument objects. */
   {
       int index;
       static CONST char *options[] = {
           "dispose",     "measure",     "names",
           "new",         "operate",     (char *) NULL
       };
       enum options {
           QUBIT_DISPOSE, QUBIT_MEASURE, QUBIT_NAMES,
           QUBIT_NEW,     QUBIT_OPERATE
       };

       if (objc < 2) {
           Tcl_WrongNumArgs(interp, 1, objv, "subcmd ?arg ...?");
           return TCL_ERROR;
       }

       if (Tcl_GetIndexFromObj(interp, objv[1], options, "subcommand",  0,
               &index) != TCL_OK) {
           return TCL_ERROR;
       }

       switch ((enum options) index) {
       case QUBIT_DISPOSE:
           return TclQubitDisposeObjCmd(clientData, interp, objc,  objv);
       case QUBIT_MEASURE:
           return TclQubitMeasureObjCmd(clientData, interp, objc,  objv);
       case QUBIT_NAMES:
           return TclQubitNamesObjCmd(clientData, interp, objc, objv);
       case QUBIT_NEW:
           return TclQubitNewObjCmd(clientData, interp, objc, objv);
       case QUBIT_OPERATE:
           return TclQubitOperateObjCmd(clientData, interp, objc,  objv);
       }

       /*
        * We won't get this far.
        */

       Tcl_Panic("unhandled subcommand");
       return TCL_ERROR;
   }
\end{verbatim}
\endgroup

A fallback definition of \textit{TclQubitNewObjCmd} that can be used when Tcl is compiled without hardware QPU support is:
\begingroup\small\begin{verbatim}
   int
   TclQubitNewObjCmd(
       ClientData dummy,           /* Not used. */
       Tcl_Interp *interp,         /* Current interpreter. */
       int objc,                   /* Number of arguments. */
       Tcl_Obj *CONST objv[])      /* Argument objects. */
   {
       int optArgIdx, index;
       static CONST char *optionStrings[] = {
           (char *) NULL           /* Currently there are no options.  */
       };

       if (objc % 2 != 0) {
           Tcl_WrongNumArgs(interp, 2, objv, "?-option value ...?");
           return TCL_ERROR;
       }
       for (optArgIdx = 2 ; optArgIdx < objc ; optArgIdx += 2) {
           if (Tcl_GetIndexFromObj(interp, objv[optArgIdx], optionStrings,
                   "option", TCL_EXACT, &index) != TCL_OK) {
               return TCL_ERROR;
           }

           /*
            * When options are added, handle them here.
            */
       }

       /*
        * Fail gracefully.
        */

       Tcl_SetErrno(ENXIO);        /* QPU not configured. */
       Tcl_AppendResult(interp, "couldn't allocate a qubit: ",
               Tcl_PosixError(interp), NULL);
       return TCL_ERROR;
   }
\end{verbatim}
\endgroup

Other fallback definitions obviously follow the same pattern. Filling in the details should be a cultivating exercise for Robert Abitbol.

\section{Copyright}

This document has been placed in the public domain.

\section{Colophon}

\textit{TIP AutoGenerator --- written by Donal K. Fellows}
\begin{thebibliography}{TIP \#1}
\addcontentsline{toc}{section}{References}
\bibitem[TIP \#131]{tip131}
Joe English,
\emph{Read My Mind and Do What I Mean},
on-line at \url{http://purl.org/tcl/tip/131.html}
\end{thebibliography}
\end{document}
