Skip to content

Commit

Permalink
Opaque declarations, and measure and reset operations.
Browse files Browse the repository at this point in the history
The instructions of QASM are classified in a hierarchy, I've tried to
implement them in the form of several enums but extending enums is not
supported in Rust (yet).

https://stackoverflow.com/questions/25214064/can-i-extend-an-enum-with-additional-values

The hierarchy is mostly related to QuantumOperation and UnitaryOperation.
A UnitaryOperation is always a QuantumOperation but not the other way around.

I went for the wrapping alternative which will make getting the value a bit
tedious. I hope to be able of implementing some traits to ease this task but
without rust-lang/rfcs#1450 it could be difficult.

In reality, this hierarchy is not strictly neccessary, I could have
grouped all the instructions in the same enum and let the grammar accept
those programas that use the different operations correctly, but I wanted
to experiment with a proper separation of instruction types. Let's see.

Perhaps, a different approach would be following this Thin Traits proposal:
http://smallcultfollowing.com/babysteps/blog/2015/10/08/virtual-structs-part-4-extended-enums-and-thin-traits/
  • Loading branch information
delapuente committed Aug 8, 2018
1 parent 0e749a1 commit 5649134
Show file tree
Hide file tree
Showing 4 changed files with 1,881 additions and 1,084 deletions.
15 changes: 11 additions & 4 deletions src/grammar/open_qasm2/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ pub struct OpenQasmProgram {
pub enum Statement {
QRegDecl(String, usize),
CRegDecl(String, usize),
GateDecl(String, Vec<String>, Vec<String>, Vec<GateOperation>)
GateDecl(String, Vec<String>, Vec<String>, Vec<GateOperation>),
OpaqueGateDecl(String, Vec<String>, Vec<String>),
QuantumOperation(QuantumOperation)
}

#[derive(Debug, PartialEq)]
Expand All @@ -18,13 +20,18 @@ pub enum GateOperation {
Barrier(Vec<String>)
}

#[derive(Debug, PartialEq)]
pub enum QuantumOperation {
Unitary(UnitaryOperation),
Measure(Argument, Argument),
Reset(Argument)
}

#[derive(Debug, PartialEq)]
pub enum UnitaryOperation {
U(Expression, Expression, Expression, Argument),
CX(Argument, Argument),
GateExpansion(String, Vec<Expression>, Vec<Argument>),
Measure(Argument, Argument),
Reset(Argument)
GateExpansion(String, Vec<Expression>, Vec<Argument>)
}

#[derive(Debug, PartialEq)]
Expand Down
22 changes: 16 additions & 6 deletions src/grammar/open_qasm2/open_qasm2.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,30 @@ pub Program: Vec<ast::Statement> = {

pub Statement: ast::Statement = {
RegisterDeclaration,
<GateDeclaration> "}" => ast::Statement::GateDecl(<>.0, <>.1, <>.2, vec![]),
<decl:GateDeclaration> <ops:GateOperationList> "}" => {
ast::Statement::GateDecl(decl.0, decl.1, decl.2, ops) }
<GateDeclaration> "{" "}" => ast::Statement::GateDecl(<>.0, <>.1, <>.2, vec![]),
<decl:GateDeclaration> "{" <ops:GateOperationList> "}" => {
ast::Statement::GateDecl(decl.0, decl.1, decl.2, ops) },
"opaque" <GateDeclaration> ";" =>
ast::Statement::OpaqueGateDecl(<>.0, <>.1, <>.2),
QuantumOperation => ast::Statement::QuantumOperation(<>)
};

RegisterDeclaration: ast::Statement = {
"qreg" <Id> "[" <Nninteger> "]" ";" => ast::Statement::QRegDecl(<>),
"creg" <Id> "[" <Nninteger> "]" ";" => ast::Statement::CRegDecl(<>)
};

QuantumOperation: ast::QuantumOperation = {
"measure" <Argument> "->" <Argument> ";" =>
ast::QuantumOperation::Measure(<>),
"reset" <Argument> ";" => ast::QuantumOperation::Reset(<>),
UnitaryOperation => ast::QuantumOperation::Unitary(<>)
};

GateDeclaration: (String, Vec<String>, Vec<String>) = {
"gate" <id:Id> <args:IdList> "{" => (id, vec![], args),
"gate" <id:Id> "(" ")" <args:IdList> "{" => (id, vec![], args),
"gate" <Id> "(" <IdList> ")" <IdList> "{" => (<>)
"gate" <id:Id> <args:IdList> => (id, vec![], args),
"gate" <id:Id> "(" ")" <args:IdList> => (id, vec![], args),
"gate" <Id> "(" <IdList> ")" <IdList> => (<>)
};

GateOperationList: Vec<ast::GateOperation> = {
Expand Down
Loading

0 comments on commit 5649134

Please sign in to comment.