Skip to content

Circumvent constructing unregistered program type nodes in ConversionGraph#986

Merged
ryanhill1 merged 6 commits intoqBraid:mainfrom
SaashaJoshi:unregister
May 29, 2025
Merged

Circumvent constructing unregistered program type nodes in ConversionGraph#986
ryanhill1 merged 6 commits intoqBraid:mainfrom
SaashaJoshi:unregister

Conversation

@SaashaJoshi
Copy link
Copy Markdown
Contributor

@SaashaJoshi SaashaJoshi commented May 28, 2025

Summary of changes

Okay, so I could figure out that the main error comes from loading the default conversions in this line,

def load_default_conversions(bias: Optional[float] = None) -> list[Conversion]:
"""
Create a list of default conversion nodes using predefined conversion functions.
Returns:
list[Conversion]: List of default conversion edges.
"""
transpiler = import_module("qbraid.transpiler.conversions")
conversion_functions: list[str] = getattr(transpiler, "conversion_functions", [])
def construct_conversion(name: str) -> Conversion:
source, target = name.split("_to_")
conversion_func = getattr(transpiler, name)
return Conversion(source, target, conversion_func, bias=bias)
return [construct_conversion(conversion) for conversion in conversion_functions]

This PR makes changes to only consider source and target in conversion_functions that are present/registered in QPROGRAM_ALIASES, hence solving the bug mentioned in #789.

Closes #789.

@SaashaJoshi SaashaJoshi requested a review from ryanhill1 as a code owner May 28, 2025 23:20
@SaashaJoshi SaashaJoshi changed the title Edit conversion_function to consider valid source and target nodes Circumvent constructing unregistered program type nodes in ConversionGraph May 28, 2025
@codecov
Copy link
Copy Markdown

codecov bot commented May 28, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

📢 Thoughts on this report? Let us know!

@SaashaJoshi
Copy link
Copy Markdown
Contributor Author

Hi @ryanhill1,

I observe that when doing the following,

unregister_program_type("qasm2")

new_graph = ConversionGraph()
print(new_graph.has_node("qasm2"))  # Shows False, should be False.

new_graph_2 = ConversionGraph(nodes=["qasm2"])
print(new_graph_2.has_node("qasm2"))    # Shows True (Is this correct?).

Could you confirm if this behavior is correct? If not, I think the issue potentially stems from not initiating nodes correctly in the ConversionGraph class. Consider this PR WIP in this case.

Copy link
Copy Markdown
Member

@ryanhill1 ryanhill1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes look good so far!

Could you add a test case to test_conversion_graph.py that verifies your solution? e.g. that creates a conversion graph, checks that a given node is present, unregisters the corresponding program type, creates a new graph, and then checks that the node is no longer included?

Also, you raised a good point when you referenced:

new_graph_2 = ConversionGraph(nodes=["qasm2"])
print(new_graph_2.has_node("qasm2")) # Shows True (Is this correct?).

I don't think one should be allowed to specify a node to use in a ConversionGraph that corresponds to a program type that isn't registered. So ideally, I think that first line should raise an exception (maybe a PackageValueError?). Although, this might require updates to existing tests in test_conversion_graph.py, which could be a bit of a hassle. If those updates end up being too involved, we can always spin this off as a separate issue—just let me know.

@SaashaJoshi
Copy link
Copy Markdown
Contributor Author

SaashaJoshi commented May 29, 2025

Okay, I am unsure why the test fails for py3.13. The tests I am running locally are on py3.10 and are successful. However, when bumping to py3.13, I find that QPROGRAM_REGISTRY and QPROGRAM_ALIASES do not have pyquil.

QPROGRAM_REGISTRY = {'openqasm3': <class 'openqasm3.ast.Program'>, 'qasm3': <class 'str'>, 'qasm2': <class 'str'>, 'qasm2_kirin': <class 'str'>, 'qubo': <class 'qbraid.programs.typer.QuboCoefficientsDict'>, 'ionq': <class 'qbraid.programs.typer.IonQDict'>}

Is this because these packages are not yet compatible with 3.13? Shall I skip the unit test for py3.13? @ryanhill1

I can do the following,

@pytest.mark.skipif(sys.version_info[:2] == (3,13), reason="Some program_types are not available for python3.13.")
@pytest.mark.parametrize("program_type", ["qasm2", "qasm3", "pyquil", "braket", "cirq"])
def test_unregistered_node_in_conversion_graph(program_type):
    """Test the unregistered nodes in ConversionGraph"""
    assert program_type in QPROGRAM_ALIASES

@ryanhill1 ryanhill1 merged commit 58c0f9d into qBraid:main May 29, 2025
8 checks passed
@SaashaJoshi SaashaJoshi deleted the unregister branch May 29, 2025 16:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] "unregistered" program type still included in ConversionGraph

2 participants