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

Add support for slice annotations #305

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 src/components/sections/CircuitSelect/FileUploader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const FileUploader = ({ onFileAccepted, isLoading }: FileUploaderProps): JSX.Ele

const { getRootProps, getInputProps, isDragActive } = useDropzone({
onDrop,
accept: [".qasm", ".json", ".lli", ".lsi"],
accept: [".qasm", ".json", ".lli", ".lsi", ".spec", ".spec.new"],
maxFiles: 1,
multiple: false,
disabled: isLoading,
Expand Down
21 changes: 9 additions & 12 deletions src/components/sections/CircuitSelect/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ const runCompilation = async (
if (emscriptenResult.exit_code === 0) {
setAppState({
compilationIsLoading: false,
apiResponse: new CompilationResultSuccess(
JSON.parse(emscriptenResult.output) as Slices,
`Emscripten Success`
),
apiResponse: new CompilationResultSuccess({
slices: JSON.parse(emscriptenResult.output) as Slices,
compilation_text:"Emscripten Success"
}),
})
} else {
setAppState({
Expand Down Expand Up @@ -107,21 +107,18 @@ const CircuitSelect = ({ appState, setAppState }: CircuitSelectProps) => {
}

const onFileAccepted = async (file: File) => {
const data = await readFile(file)
const data = (await readFile(file)) as string
const extension: string = file.name.split(".").pop() || ""
if (extension === "json") {
const json_data = JSON.parse(data as string)
if (extension === "json" || "[{".includes(data[0])) {
const json_data = JSON.parse(data)
if (Object.prototype.hasOwnProperty.call(json_data, "compilation_text")) {
setAppState({
apiResponse: new CompilationResultSuccess(
json_data.slices,
json_data.compilation_text
),
apiResponse: new CompilationResultSuccess(json_data),
compilationIsLoading: false,
})
} else {
setAppState({
apiResponse: new CompilationResultSuccess(json_data, ""),
apiResponse: new CompilationResultSuccess({compilation_text: "", slices: json_data}),
compilationIsLoading: false,
})
}
Expand Down
28 changes: 27 additions & 1 deletion src/components/sections/LatticeView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ const LatticeView = ({ compilationResult }: LatticeViewProps): JSX.Element => {
}
const [cellDimensionPixels, setCellDimensionPixels] = useState(130)
// cell font size is cellDimensionPixels divided by 20.
const { compilation_text, slices } = compilationResult
const { compilation_text, slices, slice_annotations } = compilationResult
const num_slices = slices.length

const [latticePosition, setLatticePosition] = useState({ x: 0, y: 0 })
Expand All @@ -98,6 +98,7 @@ const LatticeView = ({ compilationResult }: LatticeViewProps): JSX.Element => {

// set state of checkbox switch
const [showCompilationText, setCompilationText] = useState(false)
const [showAnnotations, setShowAnnotations] = useState(false)

// export slices to downloadable text JSON file
const exportJson = (compilationResult: CompilationResult) => {
Expand Down Expand Up @@ -143,6 +144,20 @@ const LatticeView = ({ compilationResult }: LatticeViewProps): JSX.Element => {
checked={showCompilationText}
/>
</Flex>
<Flex alignItems="center">
<FormLabel htmlFor="slice-annotations" fontSize="xl" mb="0">
Show Slice Annotations
</FormLabel>
<Switch
id="slice-annotations"
size="lg"
onChange={(e) => {
setShowAnnotations(e.target.checked)
}}
checked={showAnnotations}
// disabled={!slice_annotations}
/>
</Flex>
<Button borderWidth="2px" onClick={() => exportJson(compilationResult)}>
<Flex gap="4">
<Text fontSize="xl" margin="auto">
Expand Down Expand Up @@ -174,6 +189,17 @@ const LatticeView = ({ compilationResult }: LatticeViewProps): JSX.Element => {
</Flex>
)}

{showAnnotations && slice_annotations && slice_annotations[selectedSliceNumber] && (
<>
<Text className="line-1" fontSize="24px">
Instructions for this slice:
</Text>
<pre>
{slice_annotations[selectedSliceNumber].instuction_annotations.map(i => i.instruction_text).join("\n")}
</pre>
</>
)}

<Box pt="3" pb="3">
<Center>
<Flex flexWrap="wrap">
Expand Down
10 changes: 4 additions & 6 deletions src/lib/apiResponses.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Slices } from "./slices"
import type { CompilationResult } from "./slices"

// Different classes representing possible Api Response outcomes
export class ResponseError {
Expand All @@ -12,12 +12,10 @@ export class ResponseError {
}

export class CompilationResultSuccess {
slices: Slices
compilation_text: string
compilation_result: CompilationResult

constructor(slices: Slices, compilation_text: string) {
this.slices = slices
this.compilation_text = compilation_text
constructor(compilation_result: CompilationResult) {
this.compilation_result = compilation_result
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/lib/parseCompilationText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ const parseCompilationText = (compilation_text: string): Array<CompilationStage>
return [new CompilationStage("Compiled in browser:", "Success")]
}

if (!compilation_text.includes("Circuit"))
{
return [new CompilationStage(compilation_text, "")]
}

const compilation_text_split = compilation_text.split("Circuit")
const input_circuit = compilation_text_split[1].slice(2)

Expand Down
14 changes: 14 additions & 0 deletions src/lib/sliceAnnotations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
type InstructionAnnotation = {
instruction_text: string
}

type SliceAnnotation = {
instuction_annotations: Array<InstructionAnnotation>
}

type SliceAnnotations = Array<SliceAnnotation>

export type {
SliceAnnotation,
SliceAnnotations
}
3 changes: 3 additions & 0 deletions src/lib/slices.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {SliceAnnotations} from './sliceAnnotations'

// These types mirror the ones used by lsqecc.lattice_array and are used to interpret the calls to
// lsqecc.pipeline.json_pipeline (i.e. what you get by calling api.latticesurgery.com/compile).
// The reason for this exact mirroring is so that the output json can be generated directly from the internal python
Expand Down Expand Up @@ -68,6 +70,7 @@ type Slices = Array<Slice>

type CompilationResult = {
slices: Slices
slice_annotations?: SliceAnnotations
compilation_text: string
}

Expand Down
5 changes: 1 addition & 4 deletions src/lib/submitCompileRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,7 @@ const submitCompileRequest = async (
const responseJson = JSON.parse(response.data) as CompilationResult

setAppState({
apiResponse: new CompilationResultSuccess(
responseJson.slices,
responseJson.compilation_text
),
apiResponse: new CompilationResultSuccess(responseJson),
compilationIsLoading: false,
})
} catch (err) {
Expand Down
2 changes: 1 addition & 1 deletion src/pages/Compiler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ const CompilerPage = (): JSX.Element => {
</Text>
</Center>
{appState.apiResponse instanceof CompilationResultSuccess && (
<LatticeView compilationResult={appState.apiResponse} />
<LatticeView compilationResult={appState.apiResponse.compilation_result} />
)}
</Stack>
</>
Expand Down
Loading