qiskit_classroom.converter_model
module for ConverterModel
1""" 2 module for ConverterModel 3""" 4 5# Licensed to the Apache Software Foundation (ASF) under one 6# or more contributor license agreements. See the NOTICE file 7# distributed with this work for additional information 8# regarding copyright ownership. The ASF licenses this file 9# to you under the Apache License, Version 2.0 (the 10# "License"); you may not use this file except in compliance 11# with the License. You may obtain a copy of the License at 12# 13# http://www.apache.org/licenses/LICENSE-2.0 14# 15# Unless required by applicable law or agreed to in writing, 16# software distributed under the License is distributed on an 17# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 18# KIND, either express or implied. See the License for the 19# specific language governing permissions and limitations 20# under the License. 21 22import os 23from .expression_enum import QuantumExpression 24from .worker import ConverterWorker 25from .input_model import Input 26 27 28class ConvertingRuleException(Exception): 29 """ 30 Exception class for converting rule 31 matrix cannot convert to dirac notation directly 32 """ 33 34 def __init__(self) -> None: 35 super().__init__( 36 "Expression converting rule error: Matrix cannot convert to dirac Notation directly." 37 ) 38 39 40# pylint: disable=too-many-instance-attributes 41class ConverterModel: 42 """ 43 class for converter 44 """ 45 46 def __init__(self) -> None: 47 self.__from_expression = None 48 self.__to_expression = None 49 self.__result_img_path = "" 50 self.__input_data = None 51 self.__expression_text = "" 52 53 @property 54 def from_expression(self) -> QuantumExpression: 55 """ 56 property of __from_expression 57 """ 58 return self.__from_expression 59 60 @from_expression.setter 61 def from_expression(self, value: QuantumExpression) -> None: 62 if ( 63 value is QuantumExpression.MATRIX 64 and self.__to_expression is QuantumExpression.DIRAC 65 ): 66 raise ConvertingRuleException() 67 68 self.__from_expression = value 69 print(f"from expression changed to {value}") 70 71 @property 72 def to_expression(self) -> QuantumExpression: 73 """ 74 property of __to_expression 75 """ 76 return self.__to_expression 77 78 @to_expression.setter 79 def to_expression(self, value: QuantumExpression) -> None: 80 # cannot converte from matrix to dirac dicrectly 81 if ( 82 value is QuantumExpression.DIRAC 83 and self.__from_expression is QuantumExpression.MATRIX 84 ): 85 raise ConvertingRuleException() 86 87 self.__to_expression = value 88 print(f"to expression changed to {value}") 89 90 @property 91 def result_img_path(self) -> str: 92 """property of __result_img_path 93 94 Returns: 95 str: result_img_path 96 """ 97 98 return self.__result_img_path 99 100 @result_img_path.setter 101 def result_img_path(self, value: str) -> None: 102 self.__result_img_path = value 103 print(f"result img path change to {value}") 104 105 @property 106 def input_data(self) -> Input: 107 """property of __input_date 108 109 Returns: 110 Input: user input data 111 """ 112 return self.__input_data 113 114 @input_data.setter 115 def input_data(self, value: Input) -> None: 116 self.__input_data = value 117 print(f"input_data change to {value}") 118 119 @property 120 def expression_text(self) -> str: 121 """property of __expression_text 122 123 Returns: 124 str: expression text 125 """ 126 return self.__expression_text 127 128 @expression_text.setter 129 def expression_text(self, value: str) -> None: 130 self.__expression_text = value 131 print(f"expression_text change to {value}") 132 133 async def convert_and_draw(self, shows_result: bool) -> bool: 134 """run worker to converting expression and visualizating expression 135 136 Returns: 137 bool: if converting and drawing was success return true 138 """ 139 140 worker = ConverterWorker( 141 self.from_expression, 142 self.to_expression, 143 self.input_data, 144 self.expression_text, 145 shows_result=shows_result, 146 ) 147 img_path = await worker.run() 148 149 # replace image and remove previously generated image 150 if img_path is not None: 151 self.remove_result_img_path() 152 self.result_img_path = img_path 153 return True 154 155 def remove_result_img_path(self) -> None: 156 """remove generated img file""" 157 if self.__result_img_path is not None: 158 if os.path.isfile(self.__result_img_path): 159 os.remove(self.__result_img_path) 160 print(f"remove {self.result_img_path}")
class
ConvertingRuleException(builtins.Exception):
29class ConvertingRuleException(Exception): 30 """ 31 Exception class for converting rule 32 matrix cannot convert to dirac notation directly 33 """ 34 35 def __init__(self) -> None: 36 super().__init__( 37 "Expression converting rule error: Matrix cannot convert to dirac Notation directly." 38 )
Exception class for converting rule matrix cannot convert to dirac notation directly
Inherited Members
- builtins.BaseException
- with_traceback
- args
class
ConverterModel:
42class ConverterModel: 43 """ 44 class for converter 45 """ 46 47 def __init__(self) -> None: 48 self.__from_expression = None 49 self.__to_expression = None 50 self.__result_img_path = "" 51 self.__input_data = None 52 self.__expression_text = "" 53 54 @property 55 def from_expression(self) -> QuantumExpression: 56 """ 57 property of __from_expression 58 """ 59 return self.__from_expression 60 61 @from_expression.setter 62 def from_expression(self, value: QuantumExpression) -> None: 63 if ( 64 value is QuantumExpression.MATRIX 65 and self.__to_expression is QuantumExpression.DIRAC 66 ): 67 raise ConvertingRuleException() 68 69 self.__from_expression = value 70 print(f"from expression changed to {value}") 71 72 @property 73 def to_expression(self) -> QuantumExpression: 74 """ 75 property of __to_expression 76 """ 77 return self.__to_expression 78 79 @to_expression.setter 80 def to_expression(self, value: QuantumExpression) -> None: 81 # cannot converte from matrix to dirac dicrectly 82 if ( 83 value is QuantumExpression.DIRAC 84 and self.__from_expression is QuantumExpression.MATRIX 85 ): 86 raise ConvertingRuleException() 87 88 self.__to_expression = value 89 print(f"to expression changed to {value}") 90 91 @property 92 def result_img_path(self) -> str: 93 """property of __result_img_path 94 95 Returns: 96 str: result_img_path 97 """ 98 99 return self.__result_img_path 100 101 @result_img_path.setter 102 def result_img_path(self, value: str) -> None: 103 self.__result_img_path = value 104 print(f"result img path change to {value}") 105 106 @property 107 def input_data(self) -> Input: 108 """property of __input_date 109 110 Returns: 111 Input: user input data 112 """ 113 return self.__input_data 114 115 @input_data.setter 116 def input_data(self, value: Input) -> None: 117 self.__input_data = value 118 print(f"input_data change to {value}") 119 120 @property 121 def expression_text(self) -> str: 122 """property of __expression_text 123 124 Returns: 125 str: expression text 126 """ 127 return self.__expression_text 128 129 @expression_text.setter 130 def expression_text(self, value: str) -> None: 131 self.__expression_text = value 132 print(f"expression_text change to {value}") 133 134 async def convert_and_draw(self, shows_result: bool) -> bool: 135 """run worker to converting expression and visualizating expression 136 137 Returns: 138 bool: if converting and drawing was success return true 139 """ 140 141 worker = ConverterWorker( 142 self.from_expression, 143 self.to_expression, 144 self.input_data, 145 self.expression_text, 146 shows_result=shows_result, 147 ) 148 img_path = await worker.run() 149 150 # replace image and remove previously generated image 151 if img_path is not None: 152 self.remove_result_img_path() 153 self.result_img_path = img_path 154 return True 155 156 def remove_result_img_path(self) -> None: 157 """remove generated img file""" 158 if self.__result_img_path is not None: 159 if os.path.isfile(self.__result_img_path): 160 os.remove(self.__result_img_path) 161 print(f"remove {self.result_img_path}")
class for converter
input_data: qiskit_classroom.input_model.Input
property of __input_date
Returns: Input: user input data
async def
convert_and_draw(self, shows_result: bool) -> bool:
134 async def convert_and_draw(self, shows_result: bool) -> bool: 135 """run worker to converting expression and visualizating expression 136 137 Returns: 138 bool: if converting and drawing was success return true 139 """ 140 141 worker = ConverterWorker( 142 self.from_expression, 143 self.to_expression, 144 self.input_data, 145 self.expression_text, 146 shows_result=shows_result, 147 ) 148 img_path = await worker.run() 149 150 # replace image and remove previously generated image 151 if img_path is not None: 152 self.remove_result_img_path() 153 self.result_img_path = img_path 154 return True
run worker to converting expression and visualizating expression
Returns: bool: if converting and drawing was success return true
def
remove_result_img_path(self) -> None:
156 def remove_result_img_path(self) -> None: 157 """remove generated img file""" 158 if self.__result_img_path is not None: 159 if os.path.isfile(self.__result_img_path): 160 os.remove(self.__result_img_path) 161 print(f"remove {self.result_img_path}")
remove generated img file