Skip to content

Commit

Permalink
Merge branch 'main' into math411-patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
speller26 authored May 1, 2024
2 parents 91e385e + 971ce40 commit f10434a
Show file tree
Hide file tree
Showing 11 changed files with 155 additions and 16 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# Changelog

## v1.23.1 (2024-04-29)

### Bug Fixes and Other Changes

* Optional ctrl for `U`, add tests

## v1.23.0 (2024-04-22)

### Features

* add phaserx gate

## v1.22.0 (2024-04-16)

### Features
Expand Down
2 changes: 1 addition & 1 deletion src/braket/default_simulator/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
Version number (major.minor.patch[-label])
"""

__version__ = "1.22.1.dev0"
__version__ = "1.23.2.dev0"
1 change: 1 addition & 0 deletions src/braket/default_simulator/density_matrix_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ def properties(self) -> GateModelSimulatorDeviceCapabilities:
"ms",
"pswap",
"phaseshift",
"prx",
"rx",
"ry",
"rz",
Expand Down
35 changes: 34 additions & 1 deletion src/braket/default_simulator/gate_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,38 @@ def _cswap(instruction) -> CSwap:
return CSwap([instruction.control, *instruction.targets])


class PRx(GateOperation):
"""
PhaseRx gate.
"""

def __init__(self, targets, angle_1, angle_2, ctrl_modifiers=(), power=1):
super().__init__(
targets=targets,
ctrl_modifiers=ctrl_modifiers,
power=power,
)
self._angle_1 = angle_1
self._angle_2 = angle_2

@property
def _base_matrix(self) -> np.ndarray:
theta = self._angle_1
phi = self._angle_2
return np.array(
[
[
np.cos(theta / 2),
-1j * np.exp(-1j * phi) * np.sin(theta / 2),
],
[
-1j * np.exp(1j * phi) * np.sin(theta / 2),
np.cos(theta / 2),
],
]
)


class GPi(GateOperation):
"""
IonQ GPi gate.
Expand Down Expand Up @@ -950,7 +982,7 @@ def __init__(
theta: float,
phi: float,
lambda_: float,
ctrl_modifiers: list[int],
ctrl_modifiers: Sequence[int] = (),
power: float = 1,
):
super().__init__(
Expand Down Expand Up @@ -1035,6 +1067,7 @@ def _base_matrix(self) -> np.ndarray:
"zz": ZZ,
"ccnot": CCNot,
"cswap": CSwap,
"prx": PRx,
"gpi": GPi,
"gpi2": GPi2,
"ms": MS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
getattr(BinaryOperator, "+"): lambda x, y: IntegerLiteral(x.value + y.value),
getattr(BinaryOperator, "-"): lambda x, y: IntegerLiteral(x.value - y.value),
getattr(BinaryOperator, "*"): lambda x, y: IntegerLiteral(x.value * y.value),
getattr(BinaryOperator, "/"): lambda x, y: IntegerLiteral(x.value / y.value),
getattr(BinaryOperator, "/"): lambda x, y: IntegerLiteral(x.value // y.value),
getattr(BinaryOperator, "%"): lambda x, y: IntegerLiteral(x.value % y.value),
getattr(BinaryOperator, "**"): lambda x, y: IntegerLiteral(x.value**y.value),
getattr(UnaryOperator, "-"): lambda x: IntegerLiteral(-x.value),
Expand Down
1 change: 1 addition & 0 deletions src/braket/default_simulator/state_vector_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ def properties(self) -> GateModelSimulatorDeviceCapabilities:
"ms",
"pswap",
"phaseshift",
"prx",
"rx",
"ry",
"rz",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
PauliY,
PauliZ,
PhaseShift,
PRx,
PSwap,
RotX,
RotY,
Expand Down Expand Up @@ -92,6 +93,7 @@
("zz", ZZ, 2, (2,)),
("ccnot", CCNot, 3, ()),
("cswap", CSwap, 3, ()),
("prx", PRx, 1, (2, 3)),
("gpi", GPi, 1, (2,)),
("gpi2", GPi2, 1, (2,)),
("ms", MS, 2, (2, 3, 1.4)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1017,7 +1017,7 @@ def test_pow():
pow(two) @ cx c, a;
}
gate cxx_2 c, a {
pow(1/2) @ pow(4) @ cx c, a;
pow(1./2.) @ pow(4) @ cx c, a;
}
gate cxxx c, a {
pow(1) @ pow(two) @ cx c, a;
Expand All @@ -1029,18 +1029,19 @@ def test_pow():
qubit q4;
qubit q5;
pow(1/2) @ x q1; // half flip
pow(1/2) @ x q1; // half flip
cx q1, q2; // flip
cxx_1 q1, q3; // don't flip
cxx_2 q1, q4; // don't flip
cnot q1, q5; // flip
x q3; // flip
x q4; // flip
s q1; // sqrt z
s q1; // again
inv @ z q1; // inv z
pow(1./2) @ x q1; // half flip
pow(1/2.) @ x q1; // half flip
cx q1, q2; // flip
cxx_1 q1, q3; // don't flip
cxx_2 q1, q4; // don't flip
cnot q1, q5; // flip
x q3; // flip
x q4; // flip
pow(1/2) @ x q5; // don't flip
s q1; // sqrt z
s q1; // again
inv @ z q1; // inv z
"""
circuit = Interpreter().build_circuit(qasm)
simulation = StateVectorSimulation(5, 1, 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ def test_properties():
"ms",
"pswap",
"phaseshift",
"prx",
"rx",
"ry",
"rz",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,41 @@
[0.97766824, 0, 0, 0.02233176],
),
([gate_operations.ZZ([0, 1], 0.15)], 2, [0.99718882 - 0.07492971j, 0, 0, 0], [1, 0, 0, 0]),
(
[gate_operations.Hadamard([0]), gate_operations.GPi([0], 0.15)],
1,
[0.69916673 - 0.10566872j, 0.69916673 + 0.10566872j],
[0.5, 0.5],
),
(
[gate_operations.Hadamard([0]), gate_operations.GPi2([0], 0.15)],
1,
[0.42528093 - 0.49438554j, 0.57471907 - 0.49438554j],
[0.42528093, 0.57471907],
),
(
[gate_operations.MS([0, 1], np.pi / 2, -np.pi / 4, 0.3)],
2,
[0.98877108, 0, 0, 0.10566872 - 0.10566872j],
[0.97766824, 0, 0, 0.02233176],
),
(
[gate_operations.Hadamard([0]), gate_operations.PRx([0], 0.15, 0.4)],
1,
[0.6844863 - 0.04880085j, 0.72575165 - 0.04880085j],
[0.47090303, 0.52909697],
),
(
[gate_operations.Hadamard([0]), gate_operations.Hadamard([1]), gate_operations.ECR([0, 1])],
2,
[
0.35355339 + 0.35355339j,
0.35355339 + 0.35355339j,
0.35355339 - 0.35355339j,
0.35355339 - 0.35355339j,
],
[0.25, 0.25, 0.25, 0.25],
),
(
[
gate_operations.PauliX([0]),
Expand All @@ -155,6 +190,18 @@
[0, 0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0],
),
(
[gate_operations.Hadamard([0]), gate_operations.U([0], 0.15, 0.4, 0.7)],
1,
[0.66459511 - 0.03413278j, 0.36864009 + 0.64903989j],
[0.44285171, 0.55714829],
),
(
[gate_operations.Hadamard([0]), gate_operations.GPhase([], 0.15)],
1,
[0.69916673 + 0.10566872j, 0.69916673 + 0.10566872j],
[0.5, 0.5],
),
]

apply_observables_testdata = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ def test_properties():
"ms",
"pswap",
"phaseshift",
"prx",
"rx",
"ry",
"rz",
Expand Down Expand Up @@ -1322,3 +1323,43 @@ def test_measure_with_qubits_not_used():
assert np.sum(measurements, axis=0)[3] == 0
assert len(measurements[0]) == 4
assert result.measuredQubits == [0, 1, 2, 3]


@pytest.mark.parametrize(
"operation, state_vector",
[
["rx(π) q[0];", [0, -1j]],
["rx(pi) q[0];", [0, -1j]],
["rx(ℇ) q[0];", [0.21007866, -0.97768449j]],
["rx(euler) q[0];", [0.21007866, -0.97768449j]],
["rx(τ) q[0];", [-1, 0]],
["rx(tau) q[0];", [-1, 0]],
["rx(pi + pi) q[0];", [-1, 0]],
["rx(pi - pi) q[0];", [1, 0]],
["rx(-pi + pi) q[0];", [1, 0]],
["rx(pi * 2) q[0];", [-1, 0]],
["rx(pi / 2) q[0];", [0.70710678, -0.70710678j]],
["rx(-pi / 2) q[0];", [0.70710678, 0.70710678j]],
["rx(-pi) q[0];", [0, 1j]],
["rx(pi + 2 * pi) q[0];", [0, 1j]],
["rx(pi + pi / 2) q[0];", [-0.70710678, -0.70710678j]],
["rx((pi / 4) + (pi / 2) / 2) q[0];", [0.70710678, -0.70710678j]],
["rx(0) q[0];", [1, 0]],
["rx(0 + 0) q[0];", [1, 0]],
["rx((1.1 + 2.04) / 2) q[0];", [0.70738827, -0.70682518j]],
["rx((6 - 2.86) * 0.5) q[0];", [0.70738827, -0.70682518j]],
["rx(pi ** 2) q[0];", [0.22058404, 0.97536797j]],
],
)
def test_rotation_parameter_expressions(operation, state_vector):
qasm = f"""
OPENQASM 3.0;
bit[1] b;
qubit[1] q;
{operation}
#pragma braket result state_vector
"""
simulator = StateVectorSimulator()
result = simulator.run(OpenQASMProgram(source=qasm), shots=0)
assert result.resultTypes[0].type == StateVector()
assert np.allclose(result.resultTypes[0].value, np.array(state_vector))

0 comments on commit f10434a

Please sign in to comment.