Skip to content

Commit

Permalink
Merge pull request #223 from QuTech-Delft/allow_multiple_qubit_bit_de…
Browse files Browse the repository at this point in the history
…finitions_and_mid_circuit_measurements

Allow multiple qubit/bit (register) definitions and mid-circuit measurements
  • Loading branch information
rturrado committed May 8, 2024
2 parents dffe9a4 + 78029ca commit 9d4d6a3
Show file tree
Hide file tree
Showing 247 changed files with 4,503 additions and 579 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ jobs:
conan_profile_host: conan/profiles/tests-${{ matrix.build_type }}-apple_clang-macos-arm64
shell: bash

js-emscripten-wasm:
name: "JS tests (em++/emscripten/wasm)"
ts-emscripten-wasm:
name: "TS tests (em++/emscripten/wasm)"
runs-on: ubuntu-latest
strategy:
fail-fast: false
Expand Down Expand Up @@ -255,7 +255,7 @@ jobs:
- cpp-windows-x64
- cpp-linux-arm64
- cpp-macos-arm64
- js-emscripten-wasm
- ts-emscripten-wasm
- python-linux-x64
- python-macos-x64
- python-windows-x64
Expand Down
12 changes: 6 additions & 6 deletions emscripten/test_cqasm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ wrapper().then(function(result: any) {
}

try {
let program_1 = "version 3;qubit[5] q;H q[0:4];measure"
let program_1 = "version 3;qubit[5] q;bit[5] b;H q[0:4];b = measure"
let output = cqasm.parse_string_to_json(program_1, "shor.cq")
let expected_output = String.raw`{"errors":[{"range":{"start":{"line":1,"character":38},"end":{"line":1,"character":38}},"message":"mismatched input '<EOF>' expecting {'(', '+', '-', '~', '!', BOOLEAN_LITERAL, INTEGER_LITERAL, FLOAT_LITERAL, IDENTIFIER}","severity":1,"relatedInformation":[{"location":{"uri":"file:///shor.cq","range":{"start":{"line":0,"character":0},"end":{"line":0,"character":0}}},"message":"<unknown error message>"}]}]}`
let expected_output = String.raw`{"errors":[{"range":{"start":{"line":1,"character":51},"end":{"line":1,"character":51}},"message":"mismatched input '<EOF>' expecting {'(', '+', '-', '~', '!', BOOLEAN_LITERAL, INTEGER_LITERAL, FLOAT_LITERAL, IDENTIFIER}","severity":1,"relatedInformation":[{"location":{"uri":"file:///shor.cq","range":{"start":{"line":0,"character":0},"end":{"line":0,"character":0}}},"message":"<unknown error message>"}]}]}`
console.log( "\nExample 1:", program_1, "\n\tCalling parse_string_to_json...", "\n\tOutput:", output)
if (output !== expected_output) {
console.log("\tExpected output:", expected_output)
Expand All @@ -30,9 +30,9 @@ wrapper().then(function(result: any) {
} catch (e) { console.log(e.stack); }

try {
let program_2 = "version 3;qubit[5] q;H q[0:4];measure q"
let program_2 = "version 3;qubit[5] q;bit[5] b;H q[0:4];b = measure q"
let output = cqasm.parse_string_to_json(program_2, "<unknown>")
let expected_output = String.raw`{"Program":{"version":{"Version":{"items":"3","source_location":"<unknown>:1:9..10"}},"block":{"GlobalBlock":{"qubit_variable_declaration":{"Variable":{"name":{"Identifier":{"name":"q"}},"typ":{"Type":{"name":{"Keyword":{"name":"qubit"}},"size":{"IntegerLiteral":{"value":"5"}},"source_location":"<unknown>:1:11..19"}},"annotations":"[]","source_location":"<unknown>:1:20..21"}},"gates":[{"Gate":{"name":{"Identifier":{"name":"H"}},"operands":{"ExpressionList":{"items":[{"Index":{"expr":{"Identifier":{"name":"q"}},"indices":{"IndexList":{"items":[{"IndexRange":{"first":{"IntegerLiteral":{"value":"0","source_location":"<unknown>:1:26..27"}},"last":{"IntegerLiteral":{"value":"4","source_location":"<unknown>:1:28..29"}}}}]}},"source_location":"<unknown>:1:24..25"}}]}},"annotations":"[]","source_location":"<unknown>:1:22..23"}}],"measure_instructions":[{"MeasureInstruction":{"name":{"Identifier":{"name":"measure"}},"operand":{"Identifier":{"name":"q","source_location":"<unknown>:1:39..40"}},"annotations":"[]","source_location":"<unknown>:1:31..38"}}]}}}}`
let expected_output = String.raw`{"Program":{"version":{"Version":{"items":"3","source_location":"<unknown>:1:9..10"}},"block":{"GlobalBlock":{"statements":[{"Variable":{"name":{"Identifier":{"name":"q"}},"typ":{"Type":{"name":{"Keyword":{"name":"qubit"}},"size":{"IntegerLiteral":{"value":"5"}},"source_location":"<unknown>:1:11..19"}},"annotations":"[]","source_location":"<unknown>:1:20..21"}},{"Variable":{"name":{"Identifier":{"name":"b"}},"typ":{"Type":{"name":{"Keyword":{"name":"bit"}},"size":{"IntegerLiteral":{"value":"5"}},"source_location":"<unknown>:1:22..28"}},"annotations":"[]","source_location":"<unknown>:1:29..30"}},{"Gate":{"name":{"Identifier":{"name":"H"}},"operands":{"ExpressionList":{"items":[{"Index":{"expr":{"Identifier":{"name":"q"}},"indices":{"IndexList":{"items":[{"IndexRange":{"first":{"IntegerLiteral":{"value":"0","source_location":"<unknown>:1:35..36"}},"last":{"IntegerLiteral":{"value":"4","source_location":"<unknown>:1:37..38"}}}}]}},"source_location":"<unknown>:1:33..34"}}]}},"annotations":"[]","source_location":"<unknown>:1:31..32"}},{"MeasureInstruction":{"name":{"Identifier":{"name":"measure"}},"lhs":{"Identifier":{"name":"b","source_location":"<unknown>:1:40..41"}},"rhs":{"Identifier":{"name":"q","source_location":"<unknown>:1:52..53"}},"annotations":"[]","source_location":"<unknown>:1:44..51"}}]}}}}`
console.log("\nExample 2:", program_2, "\n\tCalling parse_string_to_json...", "\n\tOutput:", output)
if (output !== expected_output) {
console.log("\tExpected output:", expected_output)
Expand All @@ -54,9 +54,9 @@ wrapper().then(function(result: any) {
} catch (e) { console.log(e.stack); }

try {
let program_4 = "version 3;qubit[5] q;H q[0:4];measure q"
let program_4 = "version 3;qubit[5] q;bit[5] b;H q[0:4];b = measure q"
let output = cqasm.analyze_string_to_json(program_4, "spin_q.cq")
let expected_output = String.raw`{"Program":{"api_version":"3.0","version":{"Version":{"items":"3"}},"qubit_variable_declaration":{"Variable":{"name":"q","typ":{"QubitArray":{"size":"5"}},"annotations":"[]"}},"block":{"Block":{"statements":[{"Instruction":{"instruction_ref":"H(qubit array)","name":"H","operands":[{"IndexRef":{"variable":{"Variable":{"name":"q","typ":{"QubitArray":{"size":"5"}},"annotations":"[]"}},"indices":[{"ConstInt":{"value":"0"}},{"ConstInt":{"value":"1"}},{"ConstInt":{"value":"2"}},{"ConstInt":{"value":"3"}},{"ConstInt":{"value":"4"}}]}}],"annotations":"[]"}},{"Instruction":{"instruction_ref":"measure(qubit array)","name":"measure","operands":[{"VariableRef":{"variable":{"Variable":{"name":"q","typ":{"QubitArray":{"size":"5"}},"annotations":"[]"}}}}],"annotations":"[]"}}]}}}}`
let expected_output = String.raw`{"Program":{"api_version":"3.0","version":{"Version":{"items":"3"}},"block":{"Block":{"statements":[{"Instruction":{"instruction_ref":"H(qubit array)","name":"H","operands":[{"IndexRef":{"variable":{"Variable":{"name":"q","typ":{"QubitArray":{"size":"5"}},"annotations":"[]"}},"indices":[{"ConstInt":{"value":"0"}},{"ConstInt":{"value":"1"}},{"ConstInt":{"value":"2"}},{"ConstInt":{"value":"3"}},{"ConstInt":{"value":"4"}}]}}],"annotations":"[]"}},{"Instruction":{"instruction_ref":"measure(bit array, qubit array)","name":"measure","operands":[{"VariableRef":{"variable":{"Variable":{"name":"b","typ":{"BitArray":{"size":"5"}},"annotations":"[]"}}}},{"VariableRef":{"variable":{"Variable":{"name":"q","typ":{"QubitArray":{"size":"5"}},"annotations":"[]"}}}}],"annotations":"[]"}}]}},"variables":[{"Variable":{"name":"q","typ":{"QubitArray":{"size":"5"}},"annotations":"[]"}},{"Variable":{"name":"b","typ":{"BitArray":{"size":"5"}},"annotations":"[]"}}]}}`
console.log("\nExample 4:", program_4, "\n\tCalling analyze_string_to_json...", "\n\tOutput:", output)
if (output !== expected_output) {
console.log("\tExpected output:", expected_output)
Expand Down
18 changes: 18 additions & 0 deletions include/v3x/AnalyzeTreeGenAstVisitor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,27 @@ class AnalyzeTreeGenAstVisitor : public ast::Visitor<std::any> {
if (type_name == types::qubit_type_name) {
return build_semantic_type<types::Qubit, types::QubitArray>(*type, types::qubit_type_name);
}
if (type_name == types::bit_type_name) {
return build_semantic_type<types::Bit, types::BitArray>(*type, types::bit_type_name);
}
throw error::AnalysisError("unknown type \"" + type_name + "\"");
}

/**
* Convenience function for visiting a global or a local block
*/
template <typename Block>
void visit_block(Block &block) {
for (const auto &statement_ast : block.statements) {
try {
statement_ast->visit(*this);
} catch (error::AnalysisError &err) {
err.context(block);
result_.errors.push_back(std::move(err));
}
}
}

/**
* Convenience function for visiting a function call given the function's name and arguments
*/
Expand Down
11 changes: 4 additions & 7 deletions include/v3x/BuildTreeGenAstVisitor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,17 @@ class BuildTreeGenAstVisitor : public BuildCustomAstVisitor {
public:
std::any visitProgram(CqasmParser::ProgramContext *context) override;
std::any visitVersionSection(CqasmParser::VersionSectionContext *context) override;
std::any visitBodySection(CqasmParser::BodySectionContext *context) override;
std::any visitVariableDeclarationSection(CqasmParser::VariableDeclarationSectionContext *context) override;
std::any visitInstructionsSection(CqasmParser::InstructionsSectionContext *context) override;
std::any visitGatesSection(CqasmParser::GatesSectionContext *context) override;
std::any visitMeasureInstructionsSection(CqasmParser::MeasureInstructionsSectionContext *context) override;
std::any visitGlobalBlockSection(CqasmParser::GlobalBlockSectionContext *context) override;
std::any visitEofSection(CqasmParser::EofSectionContext *context) override;
std::any visitStatementSeparator(CqasmParser::StatementSeparatorContext *context) override;
std::any visitVersion(CqasmParser::VersionContext *context) override;
std::any visitVariableDeclaration(CqasmParser::VariableDeclarationContext *context) override;
std::any visitGlobalBlockStatement(CqasmParser::GlobalBlockStatementContext *context) override;
std::any visitVariableDefinition(CqasmParser::VariableDefinitionContext *context) override;
std::any visitGate(CqasmParser::GateContext *context) override;
std::any visitMeasureInstruction(CqasmParser::MeasureInstructionContext *context) override;
std::any visitGate(CqasmParser::GateContext *context) override;
std::any visitType(CqasmParser::TypeContext *context) override;
std::any visitQubitType(CqasmParser::QubitTypeContext *context) override;
std::any visitBitType(CqasmParser::BitTypeContext *context) override;
std::any visitArraySizeDeclaration(CqasmParser::ArraySizeDeclarationContext *context) override;
std::any visitExpressionList(CqasmParser::ExpressionListContext *context) override;
std::any visitIndexList(CqasmParser::IndexListContext *context) override;
Expand Down
2 changes: 2 additions & 0 deletions include/v3x/cqasm-types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
namespace cqasm::v3x::types {

constexpr const char *qubit_type_name = "qubit";
constexpr const char *bit_type_name = "bit";
constexpr const char *bool_type_name = "bool";
constexpr const char *integer_type_name = "int";
constexpr const char *float_type_name = "float";
constexpr const char *qubit_array_type_name = "qubit array";
constexpr const char *bit_array_type_name = "bit array";

/**
* A cQASM type.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
SUCCESS
Program(
version: <
Version( # input.cq:1:9..10
items: 3
)
>
block: <
GlobalBlock(
statements: [
Variable( # input.cq:3:8..9
name: <
Identifier(
name: b
)
>
typ: <
Type( # input.cq:3:1..7
name: <
Keyword(
name: bit
)
>
size: <
IntegerLiteral(
value: 0
)
>
)
>
annotations: []
)
]
)
>
)

Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
version 3

measure q
bit[0] b
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ERROR
Error at input.cq:3:8..9: found bit array of size <= 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ERROR
Error at input.cq:3:8..11: extraneous input 'abc' expecting ']'
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
version 3

bit[123abc]
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ERROR
Error at input.cq:3:8..9: missing IDENTIFIER at '\n'
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
version 3

measure 42
bit[17]
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ERROR
Error at input.cq:3:9..10: mismatched input '1' expecting IDENTIFIER
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
version 3

bit[17] 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ERROR
Error at input.cq:3:9..13: mismatched input '3.14' expecting IDENTIFIER
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
version 3

bit[17] 3.14
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
SUCCESS
Program(
version: <
Version( # input.cq:1:9..10
items: 3
)
>
block: <
GlobalBlock(
statements: [
Variable( # input.cq:3:9..10
name: <
Identifier(
name: b
)
>
typ: <
Type( # input.cq:3:1..8
name: <
Keyword(
name: bit
)
>
size: <
IntegerLiteral(
value: 17
)
>
)
>
annotations: []
)
]
)
>
)

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
version 3

bit[17] b
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
SUCCESS
Program(
api_version: 3.0
version: <
Version(
items: 3
)
>
block: <
Block(
statements: []
)
>
variables: [
Variable(
name: b
typ: <
BitArray(
size: 17
)
>
annotations: []
)
]
)

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ERROR
Error at input.cq:3:12..14: extraneous input 'b1' expecting {<EOF>, NEW_LINE, ';'}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
version 3

bit[17] b0 b1
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ERROR
Error at input.cq:3:11..12: mismatched input ',' expecting {<EOF>, NEW_LINE, ';'}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
version 3

bit[17] b0, b1
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
SUCCESS
Program(
version: <
Version( # input.cq:1:9..10
items: 3
)
>
block: <
GlobalBlock(
statements: [
Variable( # input.cq:3:9..10
name: <
Identifier(
name: b
)
>
typ: <
Type( # input.cq:3:1..8
name: <
Keyword(
name: bit
)
>
size: <
IntegerLiteral(
value: 17
)
>
)
>
annotations: []
)
Variable( # input.cq:4:9..10
name: <
Identifier(
name: b
)
>
typ: <
Type( # input.cq:4:1..8
name: <
Keyword(
name: bit
)
>
size: <
IntegerLiteral(
value: 17
)
>
)
>
annotations: []
)
]
)
>
)

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
version 3

bit[17] b
bit[17] b
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
SUCCESS
Program(
api_version: 3.0
version: <
Version(
items: 3
)
>
block: <
Block(
statements: []
)
>
variables: [
Variable(
name: b
typ: <
BitArray(
size: 17
)
>
annotations: []
)
Variable(
name: b
typ: <
BitArray(
size: 17
)
>
annotations: []
)
]
)

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ERROR
Error at input.cq:3:10..11: mismatched input '[' expecting {<EOF>, NEW_LINE, ';'}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
version 3

bit[17] b[0]
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ERROR
Error at input.cq:3:9..10: mismatched input '=' expecting IDENTIFIER
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
version 3

bit[17] =
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ERROR
Error at input.cq:3:5..9: mismatched input '3.14' expecting INTEGER_LITERAL

0 comments on commit 9d4d6a3

Please sign in to comment.