Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/assets.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
matrix:
os: [
ubuntu-latest, # Linux/x64
macos-13, # MacOS/x64
macos-15-intel, # MacOS/x64
windows-latest, # Windows/x64
ubuntu-24.04-arm, # Linux/ARM64
macos-14 # MacOS/ARM64
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ jobs:

cpp-macos-x64:
name: "C++ tests (clang/MacOS/x64)"
runs-on: macos-13 # x64
runs-on: macos-15-intel # x64
strategy:
fail-fast: false
matrix:
Expand Down Expand Up @@ -213,7 +213,7 @@ jobs:

python-macos-x64:
name: "Python tests (macOS/x64)"
runs-on: macos-13
runs-on: macos-15-intel
steps:
- name: Checkout
uses: actions/checkout@v4
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).

### Added
- `U`, `Z90`, and `mZ90` unitary instructions.
- The `measure` instruction also accepts (3 float) parameters to specify measurement axis.


## [ 1.2.1 ] - [ 2025-07-28 ]
Expand Down
2 changes: 2 additions & 0 deletions include/libqasm/v3x/instruction_set.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ class InstructionSet {
[[nodiscard]] std::optional<std::string> get_non_gate_param_types(const std::string& name) const;
[[nodiscard]] std::optional<std::string> get_gate_modifier_param_types(const std::string& name) const;
[[nodiscard]] std::optional<std::string> get_instruction_param_types(const std::string& name) const;
[[nodiscard]] std::optional<std::string> get_non_gate_param_types_with_param_count(
const std::string& name, size_t param_count) const;
};

} // namespace cqasm::v3x::instruction
2 changes: 1 addition & 1 deletion src/v3x/CqasmParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ gate:
// Current implementation of the semantic parser will expect a constant integer
// for the first expression in the WAIT instruction
nonGateInstruction:
expression EQUALS MEASURE expression # measureInstruction
expression EQUALS MEASURE (OPEN_PARENS expressionList CLOSE_PARENS)? expression # measureInstruction
| RESET expression # resetInstruction
| INIT expression # initInstruction
| BARRIER expression # barrierInstruction
Expand Down
22 changes: 22 additions & 0 deletions src/v3x/instruction_set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ InstructionSet::InstructionSet()
{ "measure", { std::nullopt, "WV" } },
{ "measure", { std::nullopt, "BV" } },
{ "measure", { std::nullopt, "WQ" } },
{ "measure", { "fff", "BQ" } },
{ "measure", { "fff", "WV" } },
{ "measure", { "fff", "BV" } },
{ "measure", { "fff", "WQ" } },
{ "reset", { std::nullopt, "Q" } },
{ "reset", { std::nullopt, "V" } },
{ "init", { std::nullopt, "Q" } },
Expand Down Expand Up @@ -248,4 +252,22 @@ InstructionSet::InstructionSet()
throw error::AnalysisError{ fmt::format("couldn't find instruction '{}'", name) };
}

[[nodiscard]] std::optional<std::string> InstructionSet::get_non_gate_param_types_with_param_count(
const std::string& name, size_t param_count) const {
const auto& range = non_gate_map.equal_range(name);
for (auto it = range.first; it != range.second; ++it) {
const auto& param_types = it->second.first;
if (!param_types.has_value()) {
if (param_count == 0) {
return param_types;
}
} else {
if (param_types->size() == param_count) {
return param_types;
}
}
}
return std::nullopt;
}

} // namespace cqasm::v3x::instruction
32 changes: 32 additions & 0 deletions src/v3x/semantic_analyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,38 @@ bool is_two_qubit_gate(const tree::One<semantic::Gate>& gate) {
values::Values resolve_parameters(const std::string& instruction_name, const values::Values& parameters) {
auto ret = values::Values{};
const auto& instruction_set = InstructionSet::get_instance();

if (instruction_set.is_non_gate(instruction_name)) {
const auto& param_types =
instruction_set.get_non_gate_param_types_with_param_count(instruction_name, parameters.size());
bool found_with_zero_params = (!param_types.has_value() && parameters.empty());

if (!found_with_zero_params && !param_types.has_value()) {
throw error::AnalysisError{ fmt::format("instruction '{}' does not have an overload with {} parameters.",
instruction_name,
parameters.size()) };
}

if (found_with_zero_params) {
return ret;
} // No parameters expected, and none provided — return empty result

if (param_types.has_value()) {
std::for_each(param_types->begin(),
param_types->end(),
[i = 0, &instruction_name, &parameters, &ret](const auto& param_type) mutable {
if (!values::check_promote(values::type_of(parameters[i]), types::from_spec(param_type))) {
throw error::AnalysisError{ fmt::format("failed to resolve '{}' with parameter type ({})",
instruction_name,
values::type_of(parameters[i])) };
}
ret.add(promote(parameters[i], types::from_spec(param_type)));
i++;
});
}
return ret;
}

const auto& param_types = instruction_set.get_instruction_param_types(instruction_name);
if (!param_types.has_value()) {
if (!parameters.empty()) {
Expand Down
4 changes: 3 additions & 1 deletion src/v3x/syntactic_analyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,9 @@ std::any SyntacticAnalyzer::visitNamedGate(CqasmParser::NamedGateContext* contex
std::any SyntacticAnalyzer::visitMeasureInstruction(CqasmParser::MeasureInstructionContext* context) {
auto ret = tree::make<NonGateInstruction>();
ret->name = tree::make<Keyword>(context->MEASURE()->getText());
ret->parameters = tree::make<ExpressionList>();
ret->parameters = context->expressionList()
? std::any_cast<One<ExpressionList>>(visitExpressionList(context->expressionList()))
: tree::make<ExpressionList>();
ret->operands = tree::make<ExpressionList>();
ret->operands->items.add(std::any_cast<One<Expression>>(context->expression(0)->accept(this)));
ret->operands->items.add(std::any_cast<One<Expression>>(context->expression(1)->accept(this)));
Expand Down
8 changes: 7 additions & 1 deletion test/v3x/cpp/test_instruction_set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class InstructionSetTest : public ::testing::Test {
const InstructionListT& two_qubit_named_gate_list = instruction_set.get_two_qubit_named_gate_list();

const size_t number_of_gate_map_entries = 60;
const size_t number_of_non_gate_map_entries = 12;
const size_t number_of_non_gate_map_entries = 16;
const size_t number_of_gate_modifier_map_entries = 3;
const size_t number_of_single_qubit_named_gates = 20;
const size_t number_of_two_qubit_named_gates = 5;
Expand Down Expand Up @@ -180,3 +180,9 @@ TEST_F(InstructionSetTest, get_instruction_param_type) {
EXPECT_THAT([this]() { (void)instruction_set.get_instruction_param_types("1q_H"); },
ThrowsMessage<AnalysisError>(::testing::HasSubstr("couldn't find instruction")));
}

TEST_F(InstructionSetTest, get_non_gate_param_types_with_param_count) {
EXPECT_EQ(instruction_set.get_non_gate_param_types_with_param_count("measure", 0), std::nullopt);
EXPECT_EQ(instruction_set.get_non_gate_param_types_with_param_count("measure", 3), "fff");
EXPECT_EQ(instruction_set.get_non_gate_param_types_with_param_count("measure", 1), std::nullopt);
}
22 changes: 21 additions & 1 deletion test/v3x/python/test_v3x_analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,27 @@


class TestV3xAnalyzer(unittest.TestCase):

def test_measure_instruction_overload(self):
program_str = "version 3;qubit q;bit b;b = measure q;b = measure(1,0,0) q"
v3x_analyzer = cq.Analyzer()
ast = v3x_analyzer.analyze_string(program_str)

measure_no_param = ast.block.statements[0]
self.assertEqual(measure_no_param.name, "measure")

measure_param = ast.block.statements[1]
self.assertEqual(measure_param.name, "measure")
self.assertEqual(measure_param.parameters[0].value, 1)
self.assertEqual(measure_param.parameters[1].value, 0)
self.assertEqual(measure_param.parameters[2].value, 0)

program_str_invalid_param = "version 3;qubit q;bit b;b = measure(1,0) q"
v3x_analyzer_invalid_param = cq.Analyzer()
errors = v3x_analyzer_invalid_param.analyze_string(program_str_invalid_param)
expected_errors = ["Error at <unknown file name>:1:29..36: instruction 'measure' does not have an overload with 2 parameters."]
self.assertEqual(errors, expected_errors)

def test_parse_string_returning_ast(self):
program_str = "version 3;qubit[5] q;bit[5] b;H q[0:4];b = measure q"
v3x_analyzer = cq.Analyzer()
Expand Down Expand Up @@ -38,7 +59,6 @@ def test_parse_string_returning_errors(self):
program_str = "version 3;qubit[5] q;bit[5] b;H q[0:4];b = measure"
v3x_analyzer = cq.Analyzer()
errors = v3x_analyzer.parse_string(program_str)
print(errors)
expected_errors = ["Error at <unknown file name>:1:51: mismatched input '<EOF>' expecting {'(', '+', '-', '~', '!', BOOLEAN_LITERAL, INTEGER_LITERAL, FLOAT_LITERAL, IDENTIFIER}"]
self.assertEqual(errors, expected_errors)

Expand Down
Loading