1. Homepage
  2. Programming
  3. FIT2014 Theory of Computation - Assignment 2- Regular Languages, Context-Free Languages, Lexical analysis, Parsing, Turing machines and Quantum Computation

FIT2014 Theory of Computation - Assignment 2- Regular Languages, Context-Free Languages, Lexical analysis, Parsing, Turing machines and Quantum Computation

Engage in a Conversation
MonashFIT2014Theory of ComputationRegular LanguagesContext-Free LanguagesLexical analysisParsingTuring machinesQuantum ComputationYACCLex

FIT2014
CourseNana.COM

Assignment 2
Regular Languages, Context-Free Languages, Lexical analysis, Parsing, Turing machines and Quantum Computation
DUE: 11:55pm, Friday 4 October 2024
CourseNana.COM

In these exercises, you will CourseNana.COM

  • implement a lexical analyser using lex (Problem 3); CourseNana.COM

  • implement parsers using lex and yacc (Problems 1–6); CourseNana.COM

  • program a Turing machine (Problem 7); CourseNana.COM

  • learn about some aspects of quantum circuits and quantum registers, by applying our methods to calculations with them (Problems 2–6); CourseNana.COM

  • practise your skills relating to the Pumping Lemmas and Context-Free Languages (Problem 8). CourseNana.COM

    Solutions to Problem 7 must be implemented in the simulator Tuatara. We are providing version 2.1 on Moodle under week 8; the file name is tuatara-monash-2.1.jar. You must use exactly this version, not some other version downloaded from the internet. Do not unpack this file. It must be run directly using the Java runtime. CourseNana.COM

    How to manage this assignment CourseNana.COM

  • You should start working on this assignment now and spread the work over the time until it is due. Do as much as possible before week 10. CourseNana.COM

  • Don’t be deterred by the length of this document! Much of it is an extended tutorial to get you started with lex and yacc (pp. 7–11) and documentation for functions, written in C, that are provided for you to use (pp. 12–17); some matrices and sample outputs also take up a fair bit of space. There is an optional three-page introduction to some of the basics of quantum computing (pp. 17–19), which is there for those who are interested in knowing more but is not required for this assignment. Although lex and yacc are new to you, the questions about them only require you to modify some existing input files for them rather than write your own input files from scratch. CourseNana.COM

  • The tasks required for the assignment are on pp. 3–6.
    For Problems 1–5, read the background material on pp. 7–11.
    For Problems 2–6, also read the background material on pp. 12–17. For Problem 7, read the background material on p. 21.
    For Problem 8, read the background material on p. 22.
    CourseNana.COM

    Instructions CourseNana.COM

    Instructions are mostly as for Assignment 1, except that some of the filenames have changed, and now each Problem has its own directory. To begin working on the assignment, download the workbench asgn2.zip from Moodle. Create a new Ed Workspace and upload this file, letting Ed automatically extract it. Edit the student-id file to contain your name and student ID. Refer to Lab 0 for a reminder on how to do these tasks. CourseNana.COM

    Open a terminal and change into the directory asgn2. You will find four other files already in the directory: plus-times-power.l, plus-times-power.y, quant.h and prob6.awk. You will not modify these files directly; you will make copies of the first two and modify the copies, while quant.h and prob6.awk must remain unaltered (although it’s ok to have copies of them in other directories) CourseNana.COM

You need to construct new lex files, using plus-times-power.l as a starting point, for Problems 1, 3, 4 & 5, and you’ll need to construct a new yacc file from plus-times-power.y for Problems 4 & 5. Your submission must include: CourseNana.COM

  • the file student-id, edited to contain your name and student ID CourseNana.COM

  • a lex file prob1.l which should be obtained by modifying a copy of plus-times-power.l CourseNana.COM

  • a PDF file prob2.pdf which should contain your solution to Problem 2 (with your name and student ID at the start) CourseNana.COM

  • a lex file prob3.l which should also be obtained by modifying a copy of plus-times-power.l CourseNana.COM

  • a lex file prob4.l which should be obtained by modifying a copy of prob3.l CourseNana.COM

  • a yacc file prob4.y which should be obtained by modifying a copy of plus-times-power.y CourseNana.COM

  • a lex file prob5.l which should be obtained by modifying a copy of prob4.l CourseNana.COM

  • a yacc file prob5.y which should be obtained by modifying a copy of prob4.y CourseNana.COM

  • a text file prob6.txt which should contain ten lines, being your solution to Problem 6 CourseNana.COM

  • a Tuatara Turing machine file prob7.tm CourseNana.COM

  • a PDF file prob8.pdf which should contain your solution to Problem 8 (with your name and student ID at the start). CourseNana.COM

    The original directory structure and file locations must be preserved. For each problem, the files you are submitting must be in the corresponding subdirectory, i.e. problemx for Problem x.
    Each of these problem subdirectories contains empty files with the required filenames. These must each be replaced by the files you write, as described above. Before submission,
    check that each of these empty files is, indeed, replaced by your own file. CourseNana.COM

    To submit your work, download the Ed workspace as a zip file by clicking on “Download All” in the file manager panel. The “Download All” option preserves the directory structure of the zip file, which is required to aid the marking process. You must submit this zip file to Moodle by the deadline given above. CourseNana.COM

Assignment tasks CourseNana.COM

First, read about “Lex, Yacc and the PLUS-TIMES-POWER language” on pp. 7–11. CourseNana.COM

Problem 1. [2 marks] CourseNana.COM

Construct prob1.l, as described on pp. 9–11, so that it can be used with plus-times-power.y to build a parser for PLUS-TIMES-POWER. CourseNana.COM

Now refer to the document “Quantum circuits, registers and the language QUANT”, pages 12–17. In fact, for Problem 2, you can focus just on pages 15–17 and the language QCIRC. CourseNana.COM

Problem 2. [3 marks] CourseNana.COM

Write a Context-Free Grammar for the language QCIRC over the eleven-symbol alphabet
{I, H, X, Y, Z, CNOT, TOF, *, , (, ) }. It can be typed or hand-written, but must be in PDF format and saved as a file prob2.pdf.pdf. CourseNana.COM

Now we use some very basic regular expressions (in the lex file, prob3.l) and a CFG (in the yacc file, prob4.y) to construct a lexical analyser (Problem 3) and a parser (Problem 4) for QCIRC. CourseNana.COM

Problem 3. [3 marks] CourseNana.COM

Using the file provided for PLUS-TIMES-POWER as a starting point, construct a lex file, prob3.l, and use it to build a lexical analyser for QCIRC. CourseNana.COM

You’ll need to introduce simple regexps for the various tokens, among other things. CourseNana.COM

Sample output: CourseNana.COM

$ ./a.out
CNOT* ( H (x)I)
Token: CNOT; Lexeme: CNOT
Token and Lexeme: *
Token and Lexeme: (
Token: H; Lexeme: H
Token: KRONECKERPROD; Lexeme: (x) Token: I; Lexeme: I
Token and Lexeme: )
Token and Lexeme: <newline>
Control-D CourseNana.COM

Problem 4. [6 marks] CourseNana.COM

Make a copy of prob3.l, call it prob4.l, then modify it so that it can be used with yacc. Then construct a yacc file prob4.y from plus-times-power.y. Then use these lex and yacc files to build a parser for QCIRC that can correctly evaluate any expression in that language. CourseNana.COM

Note that you do not have to program any of the quantum expression functions yourself. They have already been written: see the Programs section of the yacc file. The actions in your yacc file will need to call these functions, and you can do that by using the function call for pow(. . . ) in plus-times-power.y as a template. CourseNana.COM

The core of your task is to write the grammar rules in the Rules section, in yacc format, with associated actions, using the examples in plus-times-power.y as a guide. You also need to do some modifications in the Declarations section; see page 10 and further details below. CourseNana.COM

When entering your grammar into the Rules section of prob4.y, it is best to leave the existing rules for the nonterminal start unchanged, as this has some extra stuff designed to allow you to enter a series of separate expressions on separate lines. So, take the Start symbol from your grammar in Problem 2 and represent it by the nonterminal line instead of by start. CourseNana.COM

The specific modifications you need to do in the Declarations section should be: CourseNana.COM

  • You need a new %token declaration for the tokens I, H, X, Y, Z, CNOT, TOF, and KRONECKERPROD. These have the same structure as the line for the NUMBER token, except that “num” is replaced by “str” (since each of the above tokens represents a string, being names of matrices, registers or operations, whereas NUMBER represents a number). CourseNana.COM

  • You should include each of our two matrix operations, * (multiplication) and KRONECKERPROD (the Kronecker product, ), in a %left or %right statement. Such a statement specifies when an operation is left- or right-associative, i.e., whether you do multiple consecutive operations left-to-right or right-to-left. Mathematically, for * and , it doesn’t matter. So, for these, you can use either %left or %right. But you should do one of them, because it helps the parser determine an order in which to do the operations and removes some ambiguity. For operations whose %left or %right statements are on different lines, the operations with higher precedence are those with higher line numbers (i.e., later in the file). Ordinary matrix multiplication should have higher precedence than Kronecker product. CourseNana.COM

  • For nonterminal symbols, you can include a %type line that declares its type, i.e., the type of value that is returned when an expression generated from this nonterminal is evaluated. E.g., CourseNana.COM

                %type <qmx> start here
    

    Here, “qmx” is the type name we are using for quantum matrices. The various type names can be seen in the %union statement a little earlier in the file. But you do not need to know how that works in order to do this task. CourseNana.COM

  • You should still use start as your Start symbol. If you use another name instead, you will need to modify the %start line too. CourseNana.COM

Sample output: CourseNana.COM

$ ./a.out
CNOT* ( H (x)I)
4x4 matrix: CourseNana.COM

   0.7071           0.0000
   0.0000           0.7071
   0.0000           0.7071
   0.7071           0.0000
 0.7071
 0.0000
 0.0000

-0.7071 CourseNana.COM

0.0000 CourseNana.COM

 0.7071
-0.7071
 0.0000

Control-D CourseNana.COM

Problem 5. [5 marks] CourseNana.COM

Make a copy of prob4.l, call it prob5.l. Also, make a copy of prob4.y, call it prob5.y. Then modify these lex and yacc files further to build a parser for QUANT that can correctly evaluate any expression in that language. CourseNana.COM

Again, the core of your task is to write the grammar rules in the Rules section, in yacc format with associated actions, in prob5.y. You will also need new tokens KET0 and KET1, which represent k0 and k1 respectively. These tokens need appropriate rules in prob5.l and declarations in prob5.y, and you will use them in the grammar. CourseNana.COM

Problem 6. [5 marks] CourseNana.COM

In the problem6 directory you will find a file, prob6.awk. This is an awk program for converting your student ID number to a particular quantum register expression. CourseNana.COM

Do this conversion by running this awk program using awk -f prob6.awk, then (when it waits for your input) entering your student ID number. You will see the quantum register expression appear as output. CourseNana.COM

(a) Copy this quantum register expression (which will be a member of QUANT) and enter it as the first line in the text file prob6.txt. CourseNana.COM

(b) Run your parser on your expression from (a), and report the result of evaluating it by appending the output to the file prob6.txt. CourseNana.COM

The answer to (a) should be the first line of your file prob6.txt. Your answer to (b) should occupy the remaining nine lines. The file should therefore have ten lines altogether. You can use wc prob6.txt to verify this. CourseNana.COM

For example, if your ID is 12345678, then your ten-line file prob6.txt should look like this: CourseNana.COM

 (X (x) Y (x) Z) * (Y (x) CNOT) * (X (x) Y (x) Z) * (k0 (x) k0 (x) k0)
3-qubit register, 8-element vector:
   0.0000
   0.0000
   0.0000
   0.0000
   0.0000
   0.0000+1.0000i
   0.0000

0.0000 CourseNana.COM

Turing machines CourseNana.COM

Now refer to the description of the number choice function on page 21. CourseNana.COM

Problem 7. [7 marks] CourseNana.COM

Build, in Tuatara v2.1, a Turing machine that computes the number choice function, and save the Turing machine as a file prob7.tm. CourseNana.COM

Context-Free Languages CourseNana.COM

Now refer to the description of Universal Models on page 22. CourseNana.COM

Problem 8. [9 marks] CourseNana.COM

(a) Prove, using the Pumping lemma for Regular Languages, that there is no Universal Regular Expression. CourseNana.COM

(b) Prove, using the Pumping lemma for Context-Free Languages, that there is no Universal Context-Free Grammar. CourseNana.COM

Your submission can be typed or hand-written, but it must be in PDF format and saved as a single file prob8.pdf. CourseNana.COM

Lex, Yacc and the PLUS-TIMES-POWER language CourseNana.COM

In this part of the Assignment, you will use the lexical analyser generator lex, initially by itself, and then with the parser generator yacc1. CourseNana.COM

Some useful references on Lex and Yacc:
T. Niemann, Lex & Yacc Tutorial, http://epaperpress.com/lexandyacc/
Doug Brown, John Levine, and Tony Mason, lex and yacc (2nd edn.), O’Reilly, 2012. the lex and yacc manpages. CourseNana.COM

We will illustrate the use of these programs with a language PLUS-TIMES-POWER based on simple arithmetic expressions involving nonnegative integers, using just addition, multiplication and powers. Then you will use lex and yacc on some languages related to quantum computing. CourseNana.COM

PLUS-TIMES-POWER CourseNana.COM

The language PLUS-TIMES-POWER consists of expressions involving addition, multiplication and powers of nonnegative integers, without any parentheses (except for those required by the function Power). Example expressions include: CourseNana.COM

5+8, 8+5, 3+52, 13+84+Power(2,Power(3,2)), Power(1,3)+Power(5,3)+Power(3,3), Power(999,0), 0+990+1, 2014, 1014+74+101373, 235711131719. CourseNana.COM

In these expressions, integers are written in unsigned decimal, with no leading zeros or decimal point (so 2014, 86, 10, 7, and 0 are ok, but +2014, 2014, 86.0, A, 007, and 00 are not). CourseNana.COM

For lexical analysis, we treat every nonnegative integer as a lexeme for the token NUMBER. CourseNana.COM

Lex CourseNana.COM

An input file to lex is, by convention, given a name ending in .l. Such a file has three parts: definitions, CourseNana.COM

rules, CourseNana.COM

C code. CourseNana.COM

These are separated by double-percent, %%. Comments begin with /* and end with */. Any comments are ignored when lex is run on the file. CourseNana.COM

You will find an input file, plus-times-power.l, among the files for this Assignment. Study its structure now, identifying the three sections and noticing that various pieces of code have been commented out. Those pieces of code are not needed yet, but some will be needed later. CourseNana.COM

We focus mainly on the Rules section, in the middle of the file. It consists of a series of statements of the form CourseNana.COM

pattern { action } CourseNana.COM

where the pattern is a regular expression and the action consists of instructions, written in C, specifying what to do with text that matches the pattern.2 In our file, each pattern represents a set of possible lexemes which we wish to identify. These are: CourseNana.COM

1actually, Linux includes more modern implementations of these programs called flex and bison. CourseNana.COM

2This may seem reminiscent of awk, but note that: the pattern is not delimited by slashes, /.../, as in awk; the action code is in C, whereas in awk the actions are specified in awk’s own language, which has similarities with C but is not the same; and the action pertains only to the text that matches the pattern, whereas in awk the action pertains to the entire line in which the matching text is found. CourseNana.COM

a decimal representation of a nonnegative integer, represented as described above;
– This is taken to be an instance of the token NUMBER (i.e., a lexeme for that token).
CourseNana.COM

the specific string Power, which is taken to be an instance of the token POWER. certain specific characters: +, *, (, ), and comma;
the newline character;
white space, being any sequence of spaces and tabs. CourseNana.COM

Note that all matching in lex is case-sensitive.
Our
action is, in most cases, to print a message saying what token and lexeme have been found. CourseNana.COM

For white space, we take no action at all. A character that cannot be matched by any pattern yields an error message. CourseNana.COM

If you run lex on the file plus-times-power.l, then lex generates the C program lex.yy.c.3 This is the source code for the lexical analyser. You compile it using a C compiler such as cc. CourseNana.COM

For this assignment we use flex, a more modern variant of lex. We generate the lexical analyser as follows. CourseNana.COM

$ flex plus-times-power.l $ cc lex.yy.c CourseNana.COM

By default, cc puts the executable program in a file called a.out4. This can be executed in the usual way, by just entering ./a.out at the command line. If you prefer to give the executable program another name, such as plus-times-power-lex, then you can tell this to the compiler using the -o option: cc lex.yy.c -o plus-times-power-lex. CourseNana.COM

When you run the program, it will initially wait for you to input a line of text to analyse. Do so, pressing Return at the end of the line. Then the lexical analyser will print, to standard output, messages showing how it has analysed your input. The printing of these messages is done by the printf statements from the file plus-times-power.l. Note how it skips over white space, and only reports on the lexemes and tokens. CourseNana.COM

$ ./a.out
13+8 * 4 + Power(2,Power
Token: NUMBER; Lexeme: 13 Token and Lexeme: +
Token: NUMBER; Lexeme: 8 Token and Lexeme: *
Token: NUMBER; Lexeme: 4 Token and Lexeme: +
Token: POWER; Lexeme: Power Token and Lexeme: (
Token: NUMBER; Lexeme: 2 Token and Lexeme: ,
Token: POWER; Lexeme: Power Token and Lexeme: (
Token: NUMBER; Lexeme: 3 Token and Lexeme: ,
Token: NUMBER; Lexeme: 2 Token and Lexeme: )
Token and Lexeme: )
Token and Lexeme: <newline>
CourseNana.COM

(3,2 )) CourseNana.COM

Try running this program with some input expressions of your own. You can keep entering new expressions on new lines, and enter Control-D to stop when you are finished. CourseNana.COM

3The C program will have this same name, lex.yy.c, regardless of the name you gave to the lex input file. 4a.out is short for assembler output. In some other environments, it may be called a.exe. CourseNana.COM

Yacc CourseNana.COM

We now turn to parsing, using yacc.
Consider the following grammar for
PLUS-TIMES-POWER. CourseNana.COM

S −→ E
E
−→ I
E
−→ POWER(E,E) E −→ EE
E
−→ E+E CourseNana.COM

I −→ NUMBER CourseNana.COM

In this grammar, the non-terminals are S, E and I. Treat NUMBER and POWER as just single tokens, and hence single terminal symbols in this grammar. CourseNana.COM

We now generate a parser for this grammar, which will also evaluate the expressions, with +, interpreted as the usual integer arithmetic operations and Power(. . . ,. . . ) interpreted as raising its first argument to the power of its second argument. CourseNana.COM

To generate this parser, you need two files, prob1.l (for lex) and plus-times-power.y (for yacc): CourseNana.COM

  • Change into your problem1 subdirectory and do the following steps in that directory. CourseNana.COM

  • Copy plus-times-power.l to a new file prob1.l, and then modify prob1.l as follows: CourseNana.COM

    • –  in the Definitions section, uncomment the statement #include "y.tab.h"; CourseNana.COM

    • –  in the Rules section, in each action: CourseNana.COM

      • ∗  uncomment the statements of the form · yylval.str = ...;
        · yylval.num = ...;
        · return TOKENNAME; CourseNana.COM

        · return *yytext; CourseNana.COM

        · yyerror... CourseNana.COM

      • ∗  Comment out the printf statements. These may still be handy if debugging is CourseNana.COM

        needed, so don’t delete them altogether, but the lexical analyser’s main role now is to report the tokens and lexemes to the parser, not to the user. CourseNana.COM

    • –  in the C code section, comment out the function main(), which in this case occupies four lines at the end of the file. CourseNana.COM

  • plus-times-power.y, the input file for yacc, is provided for you. You don’t need to modify this yet. CourseNana.COM

    An input file for yacc is, by convention, given a name ending in .y, and has three parts, very loosely analogous to the three parts of a lex file but very different in their details and functionality: CourseNana.COM

    Declarations, Rules,
    Programs. CourseNana.COM

    These are separated by double-percent, %%. Comments begin with /* and end with */.
    Peruse the provided file
    plus-times-power.y, identify its main components, and pay particular CourseNana.COM

    attention to the following, since you will need to modify some of them later. CourseNana.COM

  • in the Declarations section: CourseNana.COM

    • –  lines like CourseNana.COM

                   int printMatrix(Matrix x);
                   Matrix identity();
      

      . CourseNana.COM

                   Register kroneckerProductReg(Register v, Register w);
      

      which are declarations of functions (but they are defined later, in the Programs section);5 CourseNana.COM

    • –  declarations of the tokens to be used: CourseNana.COM

                   %token <num> NUMBER
                   %token <str> POWER
      
    • –  some specifications that certain operations are left-associative (which helps determine the order in which operations are applied and can help resolve conflicts and ambiguities): CourseNana.COM

                   %left ’+’
                   %left ’*’
      
    • –  declarations of the nonterminal symbols to be used (which don’t need to start with an upper-case letter): CourseNana.COM

                   %type <iValue> start
                   %type <iValue> line
                   %type <iValue> expr
                   %type <iValue> int
      
    • –  nomination of which nonterminal is the Start symbol: %start start CourseNana.COM

  • in the Rules section, a list of grammar rules in Backus-Naur Form (BNF), except that the colon “:” is used instead of , and there must be a semicolon at the end of each rule. Rules with a common left-hand-side may be written in the usual compact form, by listing their right-hand-sides separated by vertical bars, and one semicolon at the very end. The terminals may be token names, in which case they must be declared in the Declarations section and also used in the lex file, or single characters enclosed in forward-quote symbols. Each rule has an action, enclosed in braces {...}. A rule for a Start symbol may print output, but most other rules will have an action of the form $$ = .... The special variable $$ represents the value to be returned for that rule, and in effect specifies how that rule is to be interpreted for evaluating the expression. The variables $1, $2, . . . refer to the values of the first, second, . . . symbols in the right-hand side of the rule. CourseNana.COM

  • in the Programs section, various functions, written in C, that your parsers will be able to use. You do not need to modify these functions, and indeed should not try to do so unless you are an experienced C programmer and know exactly what you are doing! Most of these functions are not used yet; some will only be used later, in Problem 4. CourseNana.COM

    After constructing the new lex file prob1.l as above, the parser can be generated by: CourseNana.COM

    $ yacc -d plus-times-power.y $ flex prob1.l
    $ cc lex.yy.c y.tab.c -lm CourseNana.COM

    The executable program, which is now a parser for PLUS-TIMES-POWER, is again named a.out by default, and will replace any other program of that name that is sitting in the same directory. CourseNana.COM

    5These functions for computing with quantum expressions are not needed by plus-times-power.y, but you will need them later, when you make a modified version of plus-times-power.y to parse quantum expressions. CourseNana.COM

$ ./a.out
13+8 * 4 + Power(2,Power
557
13+8*4+Power(2,Power(3,2))
557 Power(1,3)+Power(5,3)+Power(3,3) 153
1+2+3+4+5+6+7+8+9+10
55
10*9*8*7*6*5*4*3*2*1
3628800
Power(999,0)
1
Control-D CourseNana.COM

(3,2 )) CourseNana.COM

Run it with some input expressions of your own. You can keep entering new expressions on new lines, as above, and enter Control-D to stop when you are finished. CourseNana.COM

Quantum circuits, registers and the language QUANT Introduction CourseNana.COM

Roughly speaking, a quantum computer is a computer that is able to use certain capabilities based on quantum physics in addition to the usual (“classical”) capabilities that computers have. CourseNana.COM

The idea to use quantum physics for computation arose in the 1980s with work by the physicists Richard Feynman and David Deutsch. Interest increased considerably in the 1990s when Peter Shor gave an efficient quantum algorithm for integer factorisation. It had been assumed that integer factorisation was difficult; a fast factorisation algorithm could be used to break the RSA public-key cryptosystem. RSA was the first public-key cryptosystem to be published and is still the most widely used, underpinning the security of a large proportion of modern electronic communications.6. Since then, many quantum algorithms have appeared, some improving considerably on the best classical algorithms. But, to have any impact in practice, they will have to be implemented on actual quantum computers and applied to large inputs. CourseNana.COM

Several dozen quantum computers have been built. Many of these are experimental, while some have been used for highly specialised applications. But they are nowhere near powerful enough or robust enough to be useful on a large scale. Practical quantum computing still faces huge technical challenges including adequately protecting the delicate computations from being perturbed by the surrounding environment. In spite of this, there is considerable optimism about their future, and a widespread belief that, in time, their effect will be revolutionary (as well as some scepticism about this). Although they cannot yet be used to break RSA, the threat they pose has been taken very seriously by cryptographers, whose work has led to the development of new quantum- resistant cryptosystems: https://www.nist.gov/news-events/news/2022/07/nist-announces- first-four-quantum-resistant-cryptographic-algorithms. CourseNana.COM

For a recent review of the field’s current progress and prospects, see [1]. Mathematically, the heart of a quantum computer has three parts: CourseNana.COM

  • a register that stores information, and in fact can store multiple data items simultaneously, in a superposition, although these data items cannot be accessed in standard classical ways; CourseNana.COM

  • a quantum circuit expression, in which certain basic components (called gates) are combined in order to produce a transformation that is applied to the register to produce another register; CourseNana.COM

  • measurement, in which some part of a register is measured (i.e., read), but the result is de- termined probabilistically by the entire contents of the register, and the act of measurement reduces the amount of information held in the register. CourseNana.COM

    In this assignment, we just consider registers and quantum circuit expressions, and in fact we only consider restricted types of these. Nonetheless, our registers and expressions are sufficient, in prin- ciple, to represent the pre-measurement parts of quantum computations arbitrarily accurately. We define these concepts now. CourseNana.COM

    A register is a 2n-dimensional vector of unit length. It may contain complex numbers (e.g., CourseNana.COM

1). Examples of registers include: CourseNana.COM

n = 0 : CourseNana.COM

n = 0 : CourseNana.COM

1/21/2 CourseNana.COM

 1/2 CourseNana.COM

1/2 CourseNana.COM

n = 1 : CourseNana.COM

 0.6i  0.8 CourseNana.COM

0.4
6Shor also gave an efficient quantum algorithm for another number-theoretic problem called discrete log. A fast CourseNana.COM

discrete log algorithm would break the Diffie-Hellman key-exchange scheme, another important cryptographic tool. CourseNana.COM

There are two particular registers with n = 1 that we use repeatedly: CourseNana.COM

Some concrete examples:
Let
I be the usual 2 × 2 identity matrix, CourseNana.COM

traditional our name name CourseNana.COM

|0k0 |1k1 CourseNana.COM

To define quantum circuit expressions, we need two different ways of combining matrices: ordi- nary matrix multiplication, and another operation called Kronecker product which is probably new to you. We discuss these in turn. CourseNana.COM

Ordinarily, multiplying two matrices A and B is only defined if the number of columns of A equals the number of rows of B. In this assignment, we introduce a new error matrix whose sole role is to indicate that an error has been made, at some stage in a calculation, due to trying to multiply incompatible matrices. It has no rows, columns or entries, and we regard it as having n = 1. Once an error matrix appears in a calculation, you cannot get rid of it: the result of any subsequent calculation with it will again be the error matrix. With this little “hack”, we can allow any two matrices to be multiplied together; we just have to keep in mind the possibility of producing the error matrix and having it “swamp” the remainder of our current calculation. CourseNana.COM

We now define the Kronecker product. CourseNana.COM

Let A = (aij)r×c be an r×c matrix, with r rows and c columns, whose i,j-entry is aij. Similarly, let B = (bkl)s×d be an s×d matrix, with s rows and d columns, whose k,l-entry is bkl. Then the Kronecker product A B is the rs × cd matrix, with rs rows and cd columns, formed by multiplying each entry of A by a copy of B: CourseNana.COM

a11B a12B ··· a1cB CourseNana.COM

a21B a22B ··· a2cB AB = . . .. . CourseNana.COM

....ar1B ar2B · · · arcB CourseNana.COM

It can be shown that the entry in row (i1)s+k and column (j1)d+l of AB is aijbkl (where 1 i r, 1 j c, 1 k s, 1 l d). For example, if both A and B are 2 × 2 matrices, with CourseNana.COM

a11 a12  b11 b12  A=aa, B=bb, CourseNana.COM

a11 b12 a11 b22 a21 b12 a21 b22 CourseNana.COM

a12 b11 a12 b21 a22 b11 a22 b21 CourseNana.COM

a22 b12 a22 b22 CourseNana.COM

The Kronecker product can also be applied to our vectors, which are, after all, just matrices with only one column:
CourseNana.COM

0 0 CourseNana.COM

0 1k0k1= 1, k1k0= 0. CourseNana.COM

  CourseNana.COM

00
We see from these examples that Kronecker product is not commutative in general.
CourseNana.COM

Note that the Kronecker product is always defined, regardless of the dimensions of the matrices. Quantum gates CourseNana.COM

We use a small set of fundamental matrices, called quantum gates, to help build our expressions. The quantum gates we use are: CourseNana.COM

  1 1      I = 1 0 , H = 2 2 , X = 0 1 , Y = 0 i , Z = 1 0 , CourseNana.COM

1 110 i0 01 22 CourseNana.COM

10000000 CourseNana.COM

01000000 CourseNana.COM

1 0 0 0   0 0 1 0 0 0 0 0 CourseNana.COM

0 1 0 0 0 0 0 1 0 0 0 0 CNOT = 0 0 0 1 , TOF = 0 0 0 0 1 0 0 0 . CourseNana.COM

 0 0 1 0 0 0 0 0 0 1 0 0  0 0 0 0 0 0 0 1 CourseNana.COM

00000010 CourseNana.COM

This set of gates is sufficient to build a quantum circuit to approximate any quantum computation arbitrarily closely. In fact, even the two-gate set {H, TOF} is sufficient for that. CourseNana.COM

H is known as the Hadamard gate, and X, Y and Z are the Pauli-X, Pauli-Y and Pauli-Z gates. CNOT is the controlled-not gate and TOF is the Toffoli gate. The matrix Y includes the CourseNana.COM

complex numbers ±i, where i =
them with
Y in any way will usually produce some complex numbers of the form a + bi where a and b are real). CourseNana.COM

1. The other matrices are all real matrices (although combining CourseNana.COM

2 222111 1 CourseNana.COM

Languages for quantum expressions CourseNana.COM

Quantum circuit expressions are defined inductively as follows. CourseNana.COM

  • Each of I, H, X, Y, Z, CNOT and TOF is a quantum circuit expression. CourseNana.COM

  • If Q is a quantum circuit expression, then so is (Q). CourseNana.COM

  • If P and Q are quantum circuit expressions, then so is P Q. (This represents ordinary matrix multiplication.) CourseNana.COM

  • If P and Q are quantum circuit expressions, then so is P Q. (This represents Kronecker product.) CourseNana.COM

    This inductive definition can be used as the starting point for writing a Context-Free Grammar for these expressions. CourseNana.COM

    Quantum register expressions are defined inductively as follows. CourseNana.COM

  • Each of k0 and k1 is a quantum register expression. CourseNana.COM

  • If R is a quantum register expression, then so is (R). CourseNana.COM

  • If R and S are quantum register expressions, then so is R S. CourseNana.COM

  • If Q is a quantum circuit expression and R is a quantum register expression, then Q R is a quantum register expression. CourseNana.COM

    This inductive definition can also be used as the starting point for writing a Context-Free Grammar. CourseNana.COM

    The languages QCIRC, QREG and QUANT
    We define three languages to describe the types of quantum expressions we are working with. CourseNana.COM

    • QCIRC is the language, over the eleven-symbol alphabet {I, H, X, Y, Z, CNOT, TOF, *, , (, ) }, of valid quantum circuit expressions. CourseNana.COM

    • QREG is the language, over the thirteen-symbol alphabet {I, H, X, Y, Z, CNOT, TOF, k0, k1, *, , (, ) }, of valid quantum register expressions. CourseNana.COM

    • QUANT is their union: QUANT = QCIRC QREG. CourseNana.COM

      When representing members of these languages in text, we replace by the three-letter string (x), which is intended to be as close as we can get, with keyboard characters, to a cross in a circle! (Note that it uses lower-case x.) Always remember that it is our text representation of the Kronecker product symbol ; it is not an expression x enclosed in parentheses, and it is not an argument x being supplied to some function! In this assignment, we only use the lower-case letter x in this way, enclosed in one pair of parentheses in order to represent . CourseNana.COM

      In lexical analysis, we treat (x) as the sole lexeme associated with the token KRONECKERPROD. We also treat k0 and k1 as the sole lexemes associated with tokens KET0 and KET1, respectively. This follows a widespread convention that token names are upper-case. CourseNana.COM

      I, H, X, Y, Z, CNOT, TOF are tokens representing the names of the matrices we use as quantum gates. Each is also the sole lexeme for its token. CourseNana.COM

      The following table gives some members of QUANT. For each, we indicate in the right column whether it belongs to QCIRC or QREG. CourseNana.COM

quantum string evaluates CourseNana.COM

comments CourseNana.COM

QCIRC: QREG:
This is a 2 × 2 identity matrix and is a valid quantum circuit expression. But it’s not a vector, so is not a quantum register expression. CourseNana.COM

QCIRC: QREG:
This is also a valid quantum circuit ex- pression, but is not a vector. CourseNana.COM

expression CourseNana.COM

The inductive definition of a QUANT does not allow for the Kronecker product of a quantum circuit expression and a quantum register expression. (Mathematically, such a Kronecker product gives a valid matrix, but not one that represents either a quantum circuit expression or a quantum register expression.)
CourseNana.COM

This product is the wrong way round. As it is, it’s a 2 × 4 matrix, so is neither a quantum circuit expression nor a quantum register expression. For the other way round, H * k0, see previous table. CourseNana.COM

+ is not a valid operation in QUANT.
Power is not valid in QUANT; we only use it in PLUS-TIMES-POWER. CourseNana.COM

Also, commas are not used in QUANT. CourseNana.COM

k0H k0*H CourseNana.COM

H+I H+I Power(H, 2) Power(H,2) CourseNana.COM

Quantum computation CourseNana.COM

The previous sections give as much detail as you need to know, at a minimum, to do the assignment. In this section, we give a very brief introduction to some of the concepts of quantum computing. It won’t be enough to enable you to go away and write quantum algorithms. But, if you do want to learn how to do that, what you have learned from reading this section and doing this assignment will make it a bit easier to get started. CourseNana.COM

Let’s consider quantum registers again. A quantum register has 2n entries, for some nonnegative integer n. We could index them by the numbers 0, 1, . . . , 2n 1. We could also index them by strings of n bits, with these strings being the n-bit binary representations of those index numbers (with leading zeros allowed, this time). The following table shows a register with n = 2 and four CourseNana.COM


entries 1 , 0, 1 , 1 . The register is shown as a four-element vector on the right. You can verify that CourseNana.COM


its length is (|1|2 +02 +|1|2 +|1|2)1/2 = 1 + 1 + 11/2 = 11/2 = 1, as required. On the left, CourseNana.COM

we give it in tabular form. The register’s entries are in the third column (amplitude), with the first and second columns showing its indices as numbers and bit-strings respectively. CourseNana.COM

outcome amplitude probability register as vector 000 1 1 1 CourseNana.COM

222 CourseNana.COM

101 00 0 CourseNana.COM

2 10 3 11 CourseNana.COM

1 1 1 242 CourseNana.COM

111242 CourseNana.COM

The bit-strings that index the entries of the quantum register are its outcomes. The entries themselves are called the amplitudes of the outcomes.7 So, in the above register, the outcome 00 CourseNana.COM


has amplitude 1 , while outcome 11 has amplitude 1 . CourseNana.COM

A classical register, in a classical computer, stores just one bit-string. But quantum registers are very different. All the 2n outcomes, of n bits each, exist simultaneously in some sense, each with its own amplitude. Outcomes of zero amplitude (like 01 in the above example) are impossible, but all outcomes with nonzero amplitude are present to some extent. We say that the register is in a superposition of all its possible outcomes. CourseNana.COM

7We follow Lipton and Regan [5] in moving freely between (i) the index of an entry in a vector representing a register and (ii) a vector that has 1 at that index and 0 elsewhere. CourseNana.COM

In general, the amplitudes may be arbitrary complex numbers of length 1, subject to the constraint that the sum of the squares of their absolute values is 1 (so that, as a vector of 2n entries, its length is 1). CourseNana.COM

A classical register has n positions (for some n). Each position holds one bit, either 0 or 1 (but not both!). CourseNana.COM

A quantum register also has n positions (for some n), but because of superposition, the informa- tion at that position is not just one single bit. Rather, it is a superposition of bits, this superposition being induced by the register’s superposition of outcomes. This superposition of bits, in a particular position, is called a qubit, and we say that the register has n qubits. CourseNana.COM

Mathematically, classical registers may be regarded as a special case of quantum registers. A classical register containing a given bit-string is just a quantum register in which that bit-string, as an outcome, has amplitude 1 and all other outcomes have amplitude 0. For example, a two-bit classical register containing 01 is equivalent to the quantum register k0k1. Such a register is called a basis register. CourseNana.COM

We mentioned measurement on page 12 in our summary of the heart of a quantum computer. We don’t use it in this assignment, but it’s the only way we get output from a quantum computer, so let’s consider it now. CourseNana.COM

In classical computing, reading a register will recover whatever bit-string is stored in it at the time, and nothing else. Given that a quantum register contains 2n outcomes at once (in a superposition), we might hope to get much more out of it! But it’s a bit trickier than that. When we read a quantum register, we are sampling from a probability distribution over its outcomes. Each outcome has a probability of being chosen, with that probability being the square of the absolute value of its amplitude. These probabilities do indeed sum to 1, because the length of the register (as a vector) is 1. CourseNana.COM

The probabilities for each of the outcomes in our example register above are given in the fourth column of the table. If we read this register, we have a probability of 21 of getting 00. Outcomes 10 and 11 each have a probability of 14 , while outcome 01 is impossible. CourseNana.COM

For reasons related to the underlying physics, the act of reading a register is called measure- ment. CourseNana.COM

We have so far envisaged measuring (i.e., reading) an entire quantum register. But it is also possible to measure individual qubits, i.e., to read what is at a given position. Suppose we wanted to measure the second qubit in the two-qubit quantum register given above. The probability of it being 0 is the sum of the squares of the absolute values of the amplitudes of those outcomes with second bit 0: CourseNana.COM

Pr(measurement of 2nd bit gives 0) CourseNana.COM

Similarly,
Pr(measurement of 2nd bit gives
1) CourseNana.COM

A quantum computation proceeds by CourseNana.COM

= |amplitude of 00|2 + |amplitude of 10|2 CourseNana.COM


= |1|2 +|1|2 CourseNana.COM

22 = 34. CourseNana.COM

= |amplitude of 01|2 + |amplitude of 11|2 = |0|2+|21|2
= 14. CourseNana.COM

  • starting with a register with a single outcome having amplitude 1 and all other outcomes having amplitude 0 (such a register can be formed from a Kronecker product of copies of k0 and k1); CourseNana.COM

  • applying a quantum circuit — which we model as a quantum circuit expression — to the register, thereby producing a new register; CourseNana.COM

  • measuring one or more qubits of this new register. This becomes the output of the computation. 18 CourseNana.COM

The initial register is not the means of providing input to the quantum computer; it is more analo- gous to the Start State of an automaton. Input is provided to the quantum computer by using the input in the construction of the quantum circuit. So, that circuit is determined in some computable way by the input. Specification of how the circuit is computed from the input is part of the specifi- cation of a quantum algorithm. CourseNana.COM

Let’s look at a couple of very simple quantum computations. CourseNana.COM

Suppose we have n = 1 (one qubit) and start with k0. Let’s use a quantum circuit consisting solely of H. Then the computation produces CourseNana.COM

1 111Hk0=22 =2. CourseNana.COM

Then, if we measure the register, we obtain CourseNana.COM


0, CourseNana.COM

outcome=
1, CourseNana.COM

with probability with probability CourseNana.COM

12 1 2 = 2 ; CourseNana.COM

 2
1 = 1 . CourseNana.COM

CNOT(HI)(k0k0)= CourseNana.COM

So this is like tossing a fair coin.
Now suppose we use a one-qubit circuit consisting of two consecutive applications of
H. So the CourseNana.COM

quantum circuit expression is now H H. Starting again with k0, we obtain CourseNana.COM

1 1 HHk0= 2 2 CourseNana.COM

So, this time, we just end up with k0 again which has only one possible outcome. Mathematically, this is no surprise because the matrix H is its own inverse: H H = I. Let’s consider what happens in more physical terms: CourseNana.COM

  1. one application of H takes us from k0, which has only one possible outcome, to a superposition of two outcomes (so H seems to “spread out” the available amplitude across more outcomes); CourseNana.COM

  2. then another application of H — which we might intuitively expect to “spread things out” even further — actually creates “cancellation” so that only one outcome is left standing. This phenomenon is known as interference. CourseNana.COM

Now let’s graduate to two qubits and use the quantum circuit CNOT(HI) with initial register k0 k0. The final register is CourseNana.COM

11 0 2 0 2 CourseNana.COM

11
2 02 0 CourseNana.COM

 0 CourseNana.COM

 0 CourseNana.COM

CourseNana.COM

If we measure the final register, it will give outcome 00 with probability 12 and outcome 11 with probability 21 . Note that, in both these possible outcomes, the left and right bits are identical. So, in the register, the left and right qubits are not independent; in fact, they are entangled. CourseNana.COM

References CourseNana.COM

[1] Michael Brooks, Quantum computers: what are they good for?, Nature 617 (S1-S3) (24 May 2023), https://doi.org/10.1038/d41586-023-01692-9. Part of Nature Spotlight: Quantum Computing. CourseNana.COM

0222 CourseNana.COM

0 1 0 1 CourseNana.COM

1 CourseNana.COM

  1. [2]  David Deutsch, Quantum theory, the Church-Turing thesis, and the universal quantum computer, Proceedings of the Royal Society of London, Series A 400 (no. 1818) (1985) 97–117. CourseNana.COM

  2. [3]  Richard P. Feynman, Simulating physics with computers, International Journal of Theoretical Physics 21 (1982) 467–488. CourseNana.COM

  3. [4]  Richard P. Feynman, Quantum mechanical computers, Optics News 11 (February 1985) 11–20. CourseNana.COM

  4. [5]  Richard J. Lipton and Kenneth W. Regan, An Introduction to Quantum Algorithms via Linear CourseNana.COM

    Algebra (2nd edn.), MIT Press, Cambridge, Ma., USA, 2021. CourseNana.COM

  5. [6]  Michael A. Nielsen and Isaac L. Chuang, Quantum Computation and Quantum Information, Cambridge Univ. Press, 2016. CourseNana.COM

The number choice function CourseNana.COM

The number choice function is defined as follows. Input: a sequence k, x1, x2, . . . , xn where CourseNana.COM

  • k is a positive integer. CourseNana.COM

  • For each i, xi is a nonnegative integer. CourseNana.COM

  • All these numbers are encoded in unary using repetition of the letter a , with the letter b used as a separator (in effect, playing the role of the commas). So the input sequence k,x1,x2,...,xn is encoded as the string CourseNana.COM

    akbax1bax2b···axn Output: xk, i.e., the k-th of the x-numbers. CourseNana.COM

    • This also is encoded in unary in the same way, but without any b at the end. So the output xk is encoded as the string axk . CourseNana.COM

    • Recall the definition of the output of a Turing machine from Lecture 18 slide 28 (or Course Notes §18.8 p. 223). Once the machine enters the Accept state, it does not matter what comes after the leftmost blank cell; those later cells are not considered to be part of the output string. CourseNana.COM

Some example inputs and outputs: CourseNana.COM

input sequence 3,1,5,2 2,4,7,0,3
1,0,1
CourseNana.COM

1,3,2,0 3,3,2,0 0,1,5,2 4,1,5,2 CourseNana.COM

input encoded as string CourseNana.COM

output number 2
7
0
3
0
CourseNana.COM

output encoded as string CourseNana.COM

aa
aaaaaaa
ε [first tape cell must be empty] aaa
ε [first tape cell must be empty] crash: invalid input, k = 0
crash: invalid input, k > n CourseNana.COM

aaababaaaaabaa
aabaaaabaaaaaaabbaaa
abba
abaaabaab
aaabaaabaab
babaaaaabaa
undefined aaaababaaaaabaa undefined CourseNana.COM

Notes:
Any input string that is not of the specified form should be rejected, by crashing. Examples CourseNana.COM

of situations that must lead to a crash include: k = 0; k > n. CourseNana.COM

CourseNana.COM

Universal models CourseNana.COM

We know that Universal Turing Machines exist. Motivated by this, we can ask if universality also arises with the other abstract models for formal languages that we have been considering. CourseNana.COM

Universal regular expressions CourseNana.COM

A universal regular expression (URE) is a regular expression U over the nine-symbol alphabet {a,b,,,(,),ε,,$} such that, for every regular expression R over {a,b} and every string x ∈ {a, b}, CourseNana.COM

R matches x ⇐⇒ U matches R$x. CourseNana.COM

The only role of $ here is to serve as a separator between the regular expression R and the string x. This is analogous to our use of $ as a separator between an encoded Turing machine and its input, when these two are combined and given as input to a Universal Turing Machine. CourseNana.COM

For example, if R is the regular expression (a b)ba and x is the string abba then R matches x and R$x is the string (ab)ba$abba. So any URE U would have to match the string (ab)ba$abba. But if y is the string baa then R does not match y, so any URE U must not match R$y, which is (a b)ba$baa. CourseNana.COM

Universal context-free grammars CourseNana.COM

A universal context-free grammar (UCFG) is a context-free grammar U over the 18-symbol alphabet {a,b,S,T,0,1,2,3,4,5,6,7,8,9,,ε,;,$} such that, for every context-free grammar G over {a,b} and every string x ∈ {a, b}, CourseNana.COM

G generates x ⇐⇒ U generates G$x. CourseNana.COM

Again, the only role of $ is as a separator. We assume throughout that, apart from S, nonterminals have names consisting of the symbol T followed by a decimal positive integer, and that the semicolon is used as a separator between production rules in order to encode a grammar as a string. (We are not using the vertical bar when encoding grammars here.) CourseNana.COM

For example, suppose G is the CFG CourseNana.COM

This grammar can be encoded as CourseNana.COM

S aT T aTb Tε CourseNana.COM

SaT 1;T 1aT 1b;T 1ε
We have two nonterminals, S and T1, and three production rules. Suppose x is the string aaabb. CourseNana.COM

Then G$x is the string CourseNana.COM

SaT 1;T 1aT 1b;T 1ε$aaabb CourseNana.COM

Any UCFG U would have to match this string, since G generates x, but it could not match U$y where y = bb, since G cannot generate y. CourseNana.COM

Get in Touch with Our Experts

WeChat (微信) WeChat (微信)
Whatsapp WhatsApp
Monash代写,FIT2014代写,Theory of Computation代写,Regular Languages代写,Context-Free Languages代写,Lexical analysis代写,Parsing代写,Turing machines代写,Quantum Computation代写,YACC代写,Lex代写,Monash代编,FIT2014代编,Theory of Computation代编,Regular Languages代编,Context-Free Languages代编,Lexical analysis代编,Parsing代编,Turing machines代编,Quantum Computation代编,YACC代编,Lex代编,Monash代考,FIT2014代考,Theory of Computation代考,Regular Languages代考,Context-Free Languages代考,Lexical analysis代考,Parsing代考,Turing machines代考,Quantum Computation代考,YACC代考,Lex代考,Monashhelp,FIT2014help,Theory of Computationhelp,Regular Languageshelp,Context-Free Languageshelp,Lexical analysishelp,Parsinghelp,Turing machineshelp,Quantum Computationhelp,YACChelp,Lexhelp,Monash作业代写,FIT2014作业代写,Theory of Computation作业代写,Regular Languages作业代写,Context-Free Languages作业代写,Lexical analysis作业代写,Parsing作业代写,Turing machines作业代写,Quantum Computation作业代写,YACC作业代写,Lex作业代写,Monash编程代写,FIT2014编程代写,Theory of Computation编程代写,Regular Languages编程代写,Context-Free Languages编程代写,Lexical analysis编程代写,Parsing编程代写,Turing machines编程代写,Quantum Computation编程代写,YACC编程代写,Lex编程代写,Monashprogramming help,FIT2014programming help,Theory of Computationprogramming help,Regular Languagesprogramming help,Context-Free Languagesprogramming help,Lexical analysisprogramming help,Parsingprogramming help,Turing machinesprogramming help,Quantum Computationprogramming help,YACCprogramming help,Lexprogramming help,Monashassignment help,FIT2014assignment help,Theory of Computationassignment help,Regular Languagesassignment help,Context-Free Languagesassignment help,Lexical analysisassignment help,Parsingassignment help,Turing machinesassignment help,Quantum Computationassignment help,YACCassignment help,Lexassignment help,Monashsolution,FIT2014solution,Theory of Computationsolution,Regular Languagessolution,Context-Free Languagessolution,Lexical analysissolution,Parsingsolution,Turing machinessolution,Quantum Computationsolution,YACCsolution,Lexsolution,