From 3ccdce90314c7ed527e5296707340572ea6dbc95 Mon Sep 17 00:00:00 2001 From: Liam Date: Wed, 21 Aug 2024 12:26:52 +0200 Subject: [PATCH] Convert BraketSimulator.Circuit to Braket.Circuit --- .../BraketSimulatorBraketExt.jl | 1 + test/test_braket_integration.jl | 77 +++++++++++++++++++ 2 files changed, 78 insertions(+) diff --git a/ext/BraketSimulatorBraketExt/BraketSimulatorBraketExt.jl b/ext/BraketSimulatorBraketExt/BraketSimulatorBraketExt.jl index e7142cf..33fc8c0 100644 --- a/ext/BraketSimulatorBraketExt/BraketSimulatorBraketExt.jl +++ b/ext/BraketSimulatorBraketExt/BraketSimulatorBraketExt.jl @@ -153,6 +153,7 @@ function Base.convert(::Type{BraketSimulator.Program}, p::Braket.Program) end Base.convert(::Type{Braket.AbstractProgram}, p::BraketSimulator.Program) = Braket.Program(Braket.braketSchemaHeader("braket.ir.jaqcd.program", "1"), [convert(Braket.Instruction, ix) for ix in p.instructions],[convert(Braket.AbstractProgramResult, rt) for rt in p.results], [convert(Braket.Instruction, ix) for ix in p.basis_rotation_instructions]) Base.convert(::Type{Braket.AbstractProgram}, p::BraketSimulator.OpenQasmProgram) = Braket.OpenQasmProgram(Braket.braketSchemaHeader("braket.ir.openqasm.program", "1"), p.source, p.inputs) +Base.convert(::Type{Braket.Circuit}, c::BraketSimulator.Circuit) = convert(Braket.Circuit, convert(Braket.AbstractProgram, BraketSimulator.Program(c))) function __init__() Braket._simulator_devices[]["braket_dm_v2"] = diff --git a/test/test_braket_integration.jl b/test/test_braket_integration.jl index ea4c0cc..95ef247 100644 --- a/test/test_braket_integration.jl +++ b/test/test_braket_integration.jl @@ -27,6 +27,83 @@ using Braket: I, name end end +@testset "BraketSimulator to Braket Conversion" begin +@testset "Simple Circuit" begin + # Create a new circuit + circuit = Braket.Circuit() + + # Add instructions to the circuit + Braket.add_instruction!(circuit, Braket.Instruction(Braket.H(), 0)) # Apply Hadamard to qubit 0 + Braket.add_instruction!(circuit, Braket.Instruction(Braket.CZ(), 0, 1)) # Apply controlled-Z between qubit 0 and qubit 1 + Braket.add_instruction!(circuit, Braket.Instruction(Braket.Measure(), 0)) # Measure qubit 0 + Braket.add_instruction!(circuit, Braket.Instruction(Braket.Measure(), 1)) # Measure qubit 1 + + # Convert the circuit to QASM format + qasm = """ + qubit[2] q; + bit[2] c; + + h q[0]; + cz q[0], q[1]; + measure q -> c; + """ + + # Convert the QASM representation back to a Braket circuit + converted_circuit = convert(Braket.Circuit, BraketSimulator.Circuit(qasm)) + + # Assert that the original circuit and the converted circuit are equivalent + @test circuit == converted_circuit +end + +@testset "Active Reset and Mid-Circuit Measurement Circuit" begin + # Blocked by Braket.jl/issues/99 and PR #46 + # BLocked by Reset Instruction missing in Braket + # Create a new circuit using Braket + circuit = Braket.Circuit() + + # Add instructions to the circuit + Braket.add_instruction!(circuit, Braket.Instruction(Braket.X(), 2)) # Apply X gate to qubit 2 + Braket.add_instruction!(circuit, Braket.Instruction(Braket.H(), 2)) # Apply Hadamard gate to qubit 2 + Braket.add_instruction!(circuit, Braket.Instruction(Braket.H(), 1)) # Apply Hadamard gate to qubit 1 + Braket.add_instruction!(circuit, Braket.Instruction(Braket.CNot(), 1, 2)) # Apply CNot gate with qubit 1 as control and qubit 2 as target + Braket.add_instruction!(circuit, Braket.Instruction(Braket.H(), 1)) # Apply Hadamard gate to qubit 1 + Braket.add_instruction!(circuit, Braket.Instruction(Braket.Measure(), 1)) # Measure qubit 1 and store result in bit 0 + # Uncomment when Reset is added to Braket + # Braket.add_instruction!(circuit, Braket.Instruction(Braket.Reset(), 1)) # Reset qubit 1 + Braket.add_instruction!(circuit, Braket.Instruction(Braket.H(), 1)) # Apply Hadamard gate to qubit 1 again + Braket.add_instruction!(circuit, Braket.Instruction(Braket.CNot(), 1, 2)) # Apply CNot gate again + Braket.add_instruction!(circuit, Braket.Instruction(Braket.H(), 1)) # Apply Hadamard gate to qubit 1 again + Braket.add_instruction!(circuit, Braket.Instruction(Braket.Measure(), 1)) # Measure qubit 1 again and store result in bit 0 + # Uncomment when Reset is added to Braket + # Braket.add_instruction!(circuit, Braket.Instruction(Braket.Reset(), 1)) # Reset qubit 1 again + + # Convert the circuit to QASM format + qasm = """ + OPENQASM 3.0; + bit[2] b; + qubit[3] q; + x q[2]; + h q[2]; + h q[1]; + cnot q[1], q[2]; + h q[1]; + b[0] = measure q[1]; + // reset q[1]; // TODO add when Braket supports reset + h q[1]; + cnot q[1], q[2]; + h q[1]; + b[0] = measure q[1]; + // reset q[1]; // TODO add when Braket supports reset + """ + + # Convert the QASM representation back to a Braket circuit + converted_circuit = convert(Braket.Circuit, BraketSimulator.Circuit(qasm)) + + # Assert that the original circuit and the converted circuit are equivalent + @test circuit == converted_circuit +end +end + @testset "Type conversions" begin @test convert(Braket.Operator, convert(BraketSimulator.Operator, Braket.Measure(2))) == Braket.Measure(2) angle1 = 0.2