def test_three_qubit_gates(judge_log, gate): multiplier = 8 result = JudgeLogEntry(task=f"Three-qubit gate {gate}") input = cirq.unitary(gate(*(cirq.LineQubit.range(3)))) _score_and_log(input, judge_log, multiplier, result, n_qubits=3)
def test_four_qubit_gates(judge_log, gate): multiplier = 16 result = JudgeLogEntry(task=f"Four-qubit gate {gate}") input = cirq.unitary(gate(*(cirq.LineQubit.range(4)))) _score_and_log(input, judge_log, multiplier, result, n_qubits=4)
def test_sneaky_three_qubit_gate(): class SneakyThreeQubit(cirq.Gate): def _num_qubits_(self) -> int: return 2 def _unitary_(self): return cirq.unitary(cirq.CCX) def m(qs: List[cirq.GridQubit], matrix: np.ndarray) -> cirq.OP_TREE: return [SneakyThreeQubit().on(qs[0], qs[1])], [] entry = JudgeLogEntry(task="hello") score_input( m, input=cirq.unitary(cirq.CCX), result=entry, multiplier=2, n_qubits=3, min_two_qubit=0, ) expected = """executing method (0 pts): ✘ <class 'ValueError'>: cannot reshape array of size 64 into shape (2,2,2, 2+ qubit gates (0 pts): ✘ Close in trace distance (2 pts): ✘ Circuit structure (4 pts): ✘ Valid for Sycamore device (2 pts): ✘""" _assert_lines(entry, expected)
def test_simple_identity(judge_log): result = JudgeLogEntry(task="Simple identity check.") _score_and_log(np.eye(2), judge_log, 1, result, n_qubits=1, min_two_qubit=0)
def test_incrementers(judge_log, n_qubits): multiplier = 2**n_qubits result = JudgeLogEntry(task=f"{n_qubits}-qubit incrementer") input = np.empty((2**n_qubits, 2**n_qubits)) input[1:] = np.eye(2**n_qubits)[:-1] input[:1] = np.eye(2**n_qubits)[-1:] _score_and_log(input, judge_log, multiplier, result, n_qubits)
def test_random_diagonals(judge_log, n_qubits): s = np.random.RandomState(seed=42) multiplier = 2**n_qubits result = JudgeLogEntry(task=f"{n_qubits}-qubit random diagonal unitary") angles = [angle * 2 * np.pi for angle in s.rand(2**n_qubits)] input = np.diag([np.exp(1j * angle) for angle in angles]) _score_and_log(input, judge_log, multiplier, result, n_qubits)
def test_random_unitaries(judge_log, n_qubits): # TODO do some random testing with different circuit densities multiplier = 2**n_qubits * 2 s = np.random.RandomState(seed=12312422) result = JudgeLogEntry(task=f"{n_qubits}-qubit random unitary") input = cirq.testing.random_unitary(2**n_qubits, random_state=s) _score_and_log(input, judge_log, multiplier, result, n_qubits)
def test_two_qubit_gates(judge_log, gate): multiplier = 4 result = JudgeLogEntry(task=f"Two-qubit gate {gate}") input = cirq.unitary(gate(*(cirq.LineQubit.range(2)))) _score_and_log(input, judge_log, multiplier, result, n_qubits=2, min_two_qubit=1)
def test_single_qubit_gates(judge_log, gate): multiplier = 2 result = JudgeLogEntry(task=f"Single qubit gate {gate}") qs = cirq.LineQubit.range(1) input = cirq.unitary(gate(*qs)) _score_and_log(input, judge_log, multiplier, result, n_qubits=1, min_two_qubit=0)
def test_identities(judge_log, n_qubits): multiplier = 1 gate = cirq.IdentityGate(num_qubits=n_qubits) result = JudgeLogEntry(task=f"{n_qubits}-qubit identity gate {gate}") input = cirq.unitary(gate(*(cirq.LineQubit.range(n_qubits)))) _score_and_log(input, judge_log, multiplier, result, n_qubits, min_two_qubit=0)
def test_passing_case(): def m(qs: List[cirq.GridQubit], matrix: np.ndarray) -> cirq.OP_TREE: return [], [] entry = JudgeLogEntry(task="hello") score_input(m, np.eye(2), entry, 2, 1, 0) expected = """executing method (0 pts): ✔ [0 pts] Close in trace distance (2 pts): ✔ [2 pts] Circuit structure (4 pts): ✔ [4 pts] - 2-qubit gates in your result: 0 - Lower bound for general case: 0 Valid for Sycamore device (2 pts): ✔ [2 pts]""" _assert_lines(entry, expected)
def test_notimplemented_in_method(): def m(qs: List[cirq.GridQubit], matrix: np.ndarray) -> cirq.OP_TREE: return NotImplemented, [] entry = JudgeLogEntry(task="hello") score_input( m, input=np.eye(2), result=entry, multiplier=2, n_qubits=1, min_two_qubit=0 ) expected = """ executing method (0 pts): ✔ 2+ qubit gates (0 pts): [skipped] Close in trace distance (2 pts): [skipped] Circuit structure (4 pts): [skipped] Valid for Sycamore device (2 pts): [skipped]""" _assert_lines(entry, expected)
def test_error_in_method(): def m(qs: List[cirq.GridQubit], matrix: np.ndarray) -> cirq.OP_TREE: raise ValueError("bla") entry = JudgeLogEntry(task="hello") score_input( m, input=np.eye(2), result=entry, multiplier=2, n_qubits=1, min_two_qubit=0 ) expected = """ executing method (0 pts): ✘ ValueError 2+ qubit gates (0 pts): ✘ Close in trace distance (2 pts): ✘ Circuit structure (4 pts): ✘ Valid for Sycamore device (2 pts): ✘""" _assert_lines(entry, expected)
def test_fail_on_global_phase(): def m(qs: List[cirq.GridQubit], matrix: np.ndarray) -> cirq.OP_TREE: return [cirq.X(*qs)], [] entry = JudgeLogEntry(task="hello") score_input( m, input=np.eye(2), result=entry, multiplier=2, n_qubits=1, min_two_qubit=0 ) expected = """executing method (0 pts): ✔ 2+ qubit gates (0 pts): ✔ Close in trace distance (2 pts): ✘ <class 'AssertionError'>: Circuit structure (4 pts): ✘ Valid for Sycamore device (2 pts): ✘ """ _assert_lines(entry, expected)
def test_fail_on_second(): def m(qs: List[cirq.GridQubit], matrix: np.ndarray) -> cirq.OP_TREE: return [cirq.GlobalPhaseOperation(-1)], [] entry = JudgeLogEntry(task="hello") score_input( m, input=np.eye(2), result=entry, multiplier=2, n_qubits=1, min_two_qubit=0 ) expected = """executing method (0 pts): ✔ [0 pts] 2+ qubit gates (0 pts): ✔ [0 pts] Close in trace distance (2 pts): ✔ [2 pts] Circuit structure (4 pts): ✔ [4 pts] - 2-qubit gates in your result: 0 - Lower bound for general case: 0 Valid for Sycamore device (2 pts): ✘ <class 'ValueError'>: -1 is not a supported gate""" _assert_lines(entry, expected)
def test_three_qubit_gate(): def m(qs: List[cirq.GridQubit], matrix: np.ndarray) -> cirq.OP_TREE: return [cirq.CCX(*qs)], [] entry = JudgeLogEntry(task="hello") score_input( m, input=cirq.unitary(cirq.CCX), result=entry, multiplier=2, n_qubits=3, min_two_qubit=0, ) expected = """executing method (0 pts): ✔ [0 pts] 2+ qubit gates (0 pts): ✘ Number of gates that need more than two qubits: 1 <-- it should be zero! Close in trace distance (2 pts): ✘ Circuit structure (4 pts): ✘ Valid for Sycamore device (2 pts): ✘""" _assert_lines(entry, expected)