-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Ian Fan <[email protected]> Co-authored-by: Dimitri Kartsaklis <[email protected]> Co-authored-by: Neil Ortega <[email protected]>
- Loading branch information
1 parent
19da7cd
commit 00812e1
Showing
39 changed files
with
819 additions
and
177 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
""" | ||
Script for converting DisCoPy circuits to lambeq circuits. | ||
This requires `discopy>=1.1.7` to work. | ||
""" | ||
from argparse import ArgumentParser | ||
import glob | ||
import os | ||
import pickle | ||
import sys | ||
|
||
import discopy | ||
from lambeq.backend.quantum import Diagram as Circuit | ||
|
||
|
||
MIN_DISCOPY_VERSION = '1.1.7' | ||
|
||
|
||
def convert_discopy_to_lambeq(pkl_path: str) -> list[Circuit]: | ||
"""Convert pickled DisCoPy circuits from disk to lambeq circuits. | ||
Parameters | ||
---------- | ||
pkl_path: path to pickle file or directory containing pickle files | ||
Returns | ||
------- | ||
List of lambeq circuits | ||
""" | ||
|
||
if pkl_path.endswith(".pkl"): | ||
pkl_files = [pkl_path] | ||
else: | ||
pkl_files = glob.glob(pkl_path + "/*.pkl") | ||
|
||
lmbq_circs = [] | ||
for pkl_file in pkl_files: | ||
with open(pkl_file, "rb") as f: | ||
dcp_circs = pickle.load(f) | ||
|
||
if not isinstance(dcp_circs, list): | ||
dcp_circs = [dcp_circs] | ||
|
||
for dcp_circ in dcp_circs: | ||
lmbq_circ = Circuit.from_discopy(dcp_circ) | ||
lmbq_circs.append(lmbq_circ) | ||
|
||
return lmbq_circs | ||
|
||
|
||
def main(): | ||
# Conversion is only supported from DisCoPy v1.1.7 and onwards. | ||
if not discopy.__version__ >= MIN_DISCOPY_VERSION: | ||
raise AssertionError(f'`discopy>={MIN_DISCOPY_VERSION}` is required by this script.') | ||
|
||
parser = ArgumentParser(description='Convert DisCoPy circuits to lambeq circuits') | ||
parser.add_argument('--discopy-circuit-path', | ||
type=str, | ||
help='Either a directory containing the pickled DisCoPy ' | ||
'circuits or a specific pickle file.', | ||
default='discopy_circs') | ||
parser.add_argument('--output-dir', | ||
type=str, | ||
help='The directory where the output lambeq circuits ' | ||
'will be saved. The file will have the filename ' | ||
'`lambeq_circs.pkl`.', | ||
default='output') | ||
args = parser.parse_args() | ||
|
||
# Create output dir if it doesn't exist yet | ||
os.makedirs(args.output_dir, exist_ok=True) | ||
|
||
lmbq_circs = convert_discopy_to_lambeq(args.discopy_circuit_path) | ||
pickle.dump(lmbq_circs, | ||
open(f'{args.output_dir}/lambeq_circs.pkl', 'wb')) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[qiskit.ibmq] | ||
ibmqx_token = "my_API_token" | ||
|
||
[honeywell.global] | ||
user_email = "my_Honeywell/Quantinuum_account_email" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.