Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support custom gate defintions in QASM parser #6917

Merged
merged 21 commits into from
Jan 17, 2025
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
4 changes: 2 additions & 2 deletions cirq-core/cirq/circuits/qasm_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,10 +366,10 @@ def on_stuck(bad_op):
if should_annotate:
output_line_gap(1)
if isinstance(main_op, ops.GateOperation):
x = str(main_op.gate).replace('\n', '\n //')
x = str(main_op.gate).replace('\n', '\n// ')
output(f'// Gate: {x!s}\n')
else:
x = str(main_op).replace('\n', '\n //')
x = str(main_op).replace('\n', '\n// ')
output(f'// Operation: {x!s}\n')

for qasm in qasms:
Expand Down
5 changes: 5 additions & 0 deletions cirq-core/cirq/contrib/qasm_import/_lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def __init__(self):
'creg': 'CREG',
'measure': 'MEASURE',
'reset': 'RESET',
'gate': 'GATE',
'if': 'IF',
'->': 'ARROW',
'==': 'EQ',
Expand Down Expand Up @@ -120,6 +121,10 @@ def t_RESET(self, t):
r"""reset"""
return t

def t_GATE(self, t):
r"""gate"""
return t

def t_IF(self, t):
r"""if"""
return t
Expand Down
68 changes: 68 additions & 0 deletions cirq-core/cirq/contrib/qasm_import/_lexer_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,74 @@ def test_creg():
assert token.value == ";"


def test_custom_gate():
lexer = QasmLexer()
lexer.input('gate name(param1,param2) q1, q2 {X(q1)}')
token = lexer.token()
assert token.type == "GATE"
assert token.value == "gate"

token = lexer.token()
assert token.type == "ID"
assert token.value == "name"

token = lexer.token()
assert token.type == "("
assert token.value == "("

token = lexer.token()
assert token.type == "ID"
assert token.value == "param1"

token = lexer.token()
assert token.type == ","
assert token.value == ","

token = lexer.token()
assert token.type == "ID"
assert token.value == "param2"

token = lexer.token()
assert token.type == ")"
assert token.value == ")"

token = lexer.token()
assert token.type == "ID"
assert token.value == "q1"

token = lexer.token()
assert token.type == ","
assert token.value == ","

token = lexer.token()
assert token.type == "ID"
assert token.value == "q2"

token = lexer.token()
assert token.type == "{"
assert token.value == "{"

token = lexer.token()
assert token.type == "ID"
assert token.value == "X"

token = lexer.token()
assert token.type == "("
assert token.value == "("

token = lexer.token()
assert token.type == "ID"
assert token.value == "q1"

token = lexer.token()
assert token.type == ")"
assert token.value == ")"

token = lexer.token()
assert token.type == "}"
assert token.value == "}"


def test_error():
lexer = QasmLexer()
lexer.input('θ')
Expand Down
Loading