-
Notifications
You must be signed in to change notification settings - Fork 4
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
Convert BraketSimulator.Circuit to Braket.Circuit #42
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -154,6 +154,13 @@ 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) | ||
|
||
function Base.convert(::Type{Braket.Circuit}, c::BraketSimulator.Circuit) | ||
ixs = [convert(Braket.Instruction, ix) for ix in c.instructions] | ||
rts = isempty(c.result_types) ? Braket.Result[] : [convert(Braket.Result, rt) for rt in c.result_types] | ||
brs = [convert(Braket.Instruction, ix) for ix in c.basis_rotation_instructions] | ||
Braket.Circuit(Braket.Moments(), ixs, rts, brs) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might be better to use |
||
end | ||
|
||
function __init__() | ||
Braket._simulator_devices[]["braket_dm_v2"] = | ||
DensityMatrixSimulator{ComplexF64,Matrix{ComplexF64}} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,83 @@ using Braket: I, name | |
end | ||
end | ||
|
||
@testset "BraketSimulator to Braket Conversion" begin | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These are great but it might be good to have a couple smaller tests too, for example covering the non-empty results situation |
||
@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 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think you need the
isempty
check here,rts = Braket.Result[convert(Braket.Result, rt) for rt in c.result_types]
should be enough.