def state_prep_circuit(alpha, exact=False): """Returns state preparation circuit. Args: ===== alpha : numeric Parameter for state preparation circuit exact : bool If True, works with wavefunction Returns: ======== sp_circuit : cirq.Circuit State preparation circuit """ sp_circuit = Circuit() sp_circuit.append(_input_prep_gates(alpha)) if exact is False: sp_circuit.append([ MeasurementGate('r00').on(q00), MeasurementGate('r01').on(q01), MeasurementGate('r10').on(q10), MeasurementGate('r11').on(q11) ]) return sp_circuit
def compression_circuit(a, b, x, z, alpha, exact=False): """Returns compression circuit (state preparation circuit followed by encoding circuit). Args: ===== a, b, x, z : numeric Circuit parameters for encoding circuit alpha : numeric Parameter for state preparation circuit exact : bool If True, works with wavefunction Returns: ======== comp_circuit : cirq.Circuit Compression circuit """ comp_circuit = Circuit() comp_circuit.append(_input_prep_gates_stage2(alpha)) comp_circuit.append(param_CNOT(a, b, x, z, q01, q00)) comp_circuit.append(param_CNOT(a, b, x, z, q11, q10), strategy=InsertStrategy.EARLIEST) comp_circuit.append(param_CNOT(a, b, x, z, q11, q01)) if exact == False: comp_circuit.append([ MeasurementGate('r00').on(q00), MeasurementGate('r01').on(q01), MeasurementGate('r10').on(q10), MeasurementGate('r11').on(q11) ]) return comp_circuit
def test_represent_operations_in_circuit_with_measurements( circuit_type: str, rep_function, ): """Tests measurements in circuit are ignored (not represented).""" q0, q1 = LineQubit.range(2) circ_mitiq = Circuit( X(q1), MeasurementGate(num_qubits=1)(q0), X(q1), MeasurementGate(num_qubits=1)(q0), ) circ = convert_from_mitiq(circ_mitiq, circuit_type) reps = rep_function(ideal_circuit=circ, noise_level=0.1) for op in convert_to_mitiq(circ)[0].all_operations(): found = False for rep in reps: if _equal(rep.ideal, Circuit(op), require_qubit_equality=True): found = True if isinstance(op.gate, MeasurementGate): assert not found else: assert found # Number of unique gates excluding measurement gates assert len(reps) == 1
def decoder_circuit(aht, ht, zz, exact=False): """Returns latent space circuit followed by decoding circuit. Args: ===== aht, ht, zz : numeric Parameters for latent space circuit exact : bool If True, works with wavefunction Returns: ======== dc_circuit : cirq.Circuit Decoding circuit """ dc_circuit = Circuit() dc_circuit.append(_latent_space_circuit_gates(aht, ht, zz)) dc_circuit.append(param_CNOT(a, b, x, z, q11, q01)) dc_circuit.append(param_CNOT(a, b, x, z, q01, q00)) dc_circuit.append(param_CNOT(a, b, x, z, q11, q10), strategy=InsertStrategy.EARLIEST) dc_circuit.append([X(q11), X(q10)]) if exact is False: dc_circuit.append([ MeasurementGate('r00').on(q00), MeasurementGate('r01').on(q01), MeasurementGate('r10').on(q10), MeasurementGate('r11').on(q11) ]) return dc_circuit
def test_maps_measurement_gate(qubit_1): key = 'test measurement' operation = GateOperation(MeasurementGate(1, key), [qubit_1]) mapped = map_operation(operation) expected = Instruction(name='measurement', qubits=[str(qubit_1)], args={'key': key}) assert expected == mapped
def _measure_to_proto(gate: cirq.MeasurementGate, qubits: Sequence[cirq.Qid]): if len(qubits) == 0: raise ValueError('Measurement gate on no qubits.') invert_mask = None if gate.invert_mask: invert_mask = gate.invert_mask + (False, ) * (gate.num_qubits() - len(gate.invert_mask)) if invert_mask and len(invert_mask) != len(qubits): raise ValueError('Measurement gate had invert mask of length ' 'different than number of qubits it acts on.') return operations_pb2.Measurement( targets=[_qubit_to_proto(q) for q in qubits], key=cirq.measurement_key_name(gate), invert_mask=invert_mask, )
[ops.CNOT.on(qreg[0], qreg[2])], ) assert _equal(copy, correct) _append_measurements(copy, measurements) assert _equal(copy, circ) @pytest.mark.parametrize("gate", [X ** 3, Y ** -3, Z ** -1, H ** -1]) def test_simplify_gate_exponent(gate): # Check exponent is simplified to 1 assert _simplify_gate_exponent(gate).exponent == 1 # Check simplified gate is equivalent to the input assert _simplify_gate_exponent(gate) == gate @pytest.mark.parametrize("gate", [T ** -1, S ** -1, MeasurementGate(1)]) def test_simplify_gate_exponent_with_gates_that_cannot_be_simplified(gate): # Check the gate is not simplified (same representation) assert _simplify_gate_exponent(gate).__repr__() == gate.__repr__() def test_simplify_circuit_exponents_controlled_gate(): circuit = Circuit( ControlledGate(CNOT, num_controls=1).on(*LineQubit.range(3)) ) copy = circuit.copy() _simplify_circuit_exponents(circuit) assert _equal(circuit, copy)
def test_raises_error_for_non_trivial_invert_mask(qubit_1, qubit_2): operation = GateOperation( MeasurementGate(2, 'measurement key', invert_mask=(True, False)), [qubit_1, qubit_2]) with pytest.raises(OperationNotSupportedError): map_operation(operation)