Skip to content

Commit

Permalink
Support declaring single hardware qubits
Browse files Browse the repository at this point in the history
Statements like `qubit $1;` are supported.

This commit also undoes the previous commit (last commit before tagging 0.3.0) that
improved the error handling for attemted declaration of hardware qubits.

API CHANGE: new variant in asg::Stmt
The variant is  `Stmt::DeclareHardwareQubit(DeclareHardwareQubit)`

The struct `DeclareHardwareQubit` has method `name` to retrieve an instance of
`asg::HardwareQubit`. Note that `asg::HardwareQubit` is not new in this
commit.
  • Loading branch information
jlapeyre committed Mar 1, 2024
1 parent fba5539 commit bf996af
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 6 deletions.
3 changes: 3 additions & 0 deletions crates/oq3_parser/src/grammar/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,9 @@ pub(crate) fn qubit_type_spec(p: &mut Parser<'_>) -> bool {
type_name(p);
if p.at(T!['[']) {
designator(p);
if p.at(HARDWAREIDENT) {
p.error("Found designator in hardware qubit declaration.");
}
}
m.complete(p, QUBIT_TYPE);
true
Expand Down
5 changes: 3 additions & 2 deletions crates/oq3_parser/src/grammar/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,10 @@ fn for_stmt(p: &mut Parser<'_>, m: Marker) {
fn qubit_declaration_stmt(p: &mut Parser<'_>, m: Marker) {
assert!(p.at(T![qubit]));
expressions::qubit_type_spec(p);
// Declaring hardware qubits is forbidden in the OQ3 spec.
// But it is used by all the current users of openqasm3_parser.
if p.at(HARDWAREIDENT) {
p.bump_any();
p.error("Declaring a hardware qubit is not allowed.");
expressions::atom::hardware_qubit(p);
} else {
expressions::var_name(p);
}
Expand Down
24 changes: 24 additions & 0 deletions crates/oq3_semantics/src/asg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ pub enum Stmt {
Continue,
DeclareClassical(Box<DeclareClassical>),
DeclareQuantum(DeclareQuantum),
DeclareHardwareQubit(DeclareHardwareQubit),
Def, // stub
DefCal, // stub
Delay(DelayStmt),
Expand Down Expand Up @@ -493,6 +494,29 @@ impl DeclareQuantum {
pub fn name(&self) -> &SymbolIdResult {
&self.name
}

pub fn to_stmt(self) -> Stmt {
Stmt::DeclareQuantum(self)
}
}

#[derive(Clone, Debug, PartialEq)]
pub struct DeclareHardwareQubit {
name: HardwareQubit,
}

impl DeclareHardwareQubit {
pub fn new(name: HardwareQubit) -> DeclareHardwareQubit {
DeclareHardwareQubit { name }
}

pub fn name(&self) -> &HardwareQubit {
&self.name
}

pub fn to_stmt(self) -> Stmt {
Stmt::DeclareHardwareQubit(self)
}
}

#[derive(Clone, Debug, PartialEq)]
Expand Down
10 changes: 7 additions & 3 deletions crates/oq3_semantics/src/syntax_to_semantics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,12 @@ fn from_stmt(stmt: synast::Stmt, context: &mut Context) -> Option<asg::Stmt> {
}

synast::Stmt::QuantumDeclarationStatement(q_decl) => {
let name_str = if let Some(name_str) = q_decl.name() {
name_str.string()
} else {
let hw_qubit = q_decl.hardware_qubit().unwrap();
return Some(asg::DeclareHardwareQubit::new(ast_hardware_qubit(&hw_qubit)).to_stmt());
};
let qubit_type = q_decl.qubit_type().unwrap();
let width = match qubit_type.designator().and_then(|x| x.expr()) {
Some(synast::Expr::Literal(ref literal)) => {
Expand All @@ -288,10 +294,8 @@ fn from_stmt(stmt: synast::Stmt, context: &mut Context) -> Option<asg::Stmt> {
Some(width) => Type::QubitArray(ArrayDims::D1(width as usize)),
None => Type::Qubit,
};
let name_str = q_decl.name().unwrap().string();
let symbol_id = context.new_binding(name_str.as_ref(), &typ, &q_decl);
let q_decl_ast = asg::DeclareQuantum::new(symbol_id);
Some(asg::Stmt::DeclareQuantum(q_decl_ast))
Some(asg::DeclareQuantum::new(symbol_id).to_stmt())
}

synast::Stmt::AssignmentStmt(assignment_stmt) => {
Expand Down
2 changes: 1 addition & 1 deletion crates/oq3_syntax/openqasm3.ungram
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ OldStyleDeclarationStatement =
('creg' | 'qreg') Name Designator? ';'

QuantumDeclarationStatement =
QubitType Name ';'
QubitType (Name | HardwareQubit) ';'

AssignmentStmt =
(Name | IndexedIdentifier) '=' rhs:Expr ';'
3 changes: 3 additions & 0 deletions crates/oq3_syntax/src/ast/generated/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,9 @@ impl QuantumDeclarationStatement {
pub fn qubit_type(&self) -> Option<QubitType> {
support::child(&self.syntax)
}
pub fn hardware_qubit(&self) -> Option<HardwareQubit> {
support::child(&self.syntax)
}
pub fn semicolon_token(&self) -> Option<SyntaxToken> {
support::token(&self.syntax, T![;])
}
Expand Down

0 comments on commit bf996af

Please sign in to comment.